Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created February 22, 2017 04:43
Show Gist options
  • Select an option

  • Save sminnee/f6f83788a9225fd36eb6335b8a275ad7 to your computer and use it in GitHub Desktop.

Select an option

Save sminnee/f6f83788a9225fd36eb6335b8a275ad7 to your computer and use it in GitHub Desktop.
diff --git a/main.php b/main.php
index daa214c..dcbc9de 100644
--- a/main.php
+++ b/main.php
@@ -149,10 +149,6 @@ $chain
// Load in core
require_once('Core/Core.php');
- // Connect to database
- global $databaseConfig;
- if ($databaseConfig) DB::connect($databaseConfig);
-
// Check if a token is requesting a redirect
if (!$reloadToken) return;
diff --git a/src/ORM/Connect/MySQLDatabase.php b/src/ORM/Connect/MySQLDatabase.php
index 8201bed..0490df5 100644
--- a/src/ORM/Connect/MySQLDatabase.php
+++ b/src/ORM/Connect/MySQLDatabase.php
@@ -57,6 +57,7 @@ class MySQLDatabase extends Database
public function connect($parameters)
{
+ print_r($parameters);
// Ensure that driver is available (required by PDO)
if (empty($parameters['driver'])) {
$parameters['driver'] = $this->getDatabaseServer();
diff --git a/src/ORM/DB.php b/src/ORM/DB.php
index fc1b759..b352c54 100644
--- a/src/ORM/DB.php
+++ b/src/ORM/DB.php
@@ -62,7 +62,7 @@ class DB
*/
public static function set_conn(Database $connection, $name = 'default')
{
- self::$connections[$name] = $connection;
+ throw new \LogicException("DB::set_conn() no longer works. Configure your database vai Injector config");
}
/**
@@ -74,10 +74,8 @@ class DB
*/
public static function get_conn($name = 'default')
{
- if (isset(self::$connections[$name])) {
- return self::$connections[$name];
- }
- return null;
+ $service = Database::class . ($name === 'default' ? '' : ".$name");
+ return Injector::inst()->get($service);
}
/**
@@ -259,28 +257,13 @@ class DB
*/
public static function connect($databaseConfig, $label = 'default')
{
+ throw new \LogicException("DB::connect() no longer works. Register a SilverStripe\ORM\Connect\Database service instead");
// This is used by the "testsession" module to test up a test session using an alternative name
+ // TO DO : Replace this
if ($name = self::get_alternative_database_name()) {
$databaseConfig['database'] = $name;
}
-
- if (!isset($databaseConfig['type']) || empty($databaseConfig['type'])) {
- user_error("DB::connect: Not passed a valid database config", E_USER_ERROR);
- }
-
- self::$connection_attempted = true;
-
- $dbClass = $databaseConfig['type'];
-
- // Using Injector->create allows us to use registered configurations
- // which may or may not map to explicit objects
- $conn = Injector::inst()->create($dbClass);
- $conn->connect($databaseConfig);
-
- self::set_conn($conn, $label);
-
- return $conn;
}
/**
@@ -290,7 +273,7 @@ class DB
*/
public static function connection_attempted()
{
- return self::$connection_attempted;
+ throw new \LogicException("DB::connection_attempted() no longer works.");
}
/**
diff --git a/tests/php/ORM/DBMoneyTest.php b/tests/php/ORM/DBMoneyTest.php
index 5a99e4b..ee710c2 100644
--- a/tests/php/ORM/DBMoneyTest.php
+++ b/tests/php/ORM/DBMoneyTest.php
@@ -48,7 +48,20 @@ class DBMoneyTest extends SapphireTest
$changed = $obj->getChangedFields();
$this->assertContains('MyMoney', array_keys($changed), 'Field is detected as changed');
$this->assertEquals(2, $changed['MyMoney']['level'], 'Correct change level');
- }
+
+
+ // Reset after write
+ $obj->write();
+ $changed = $obj->getChangedFields();
+ $this->assertNotContains('MyMoney', array_keys($changed), 'Field is not detected as changed after write');
+
+ // With changes to other fields
+ DataObject::flush_and_destroy_cache();
+ $obj = DataObject::get_by_id(DBMoneyTest\TestObject::class, $obj->ID, false);
+ $obj->MyTest = 'foo';
+ $changed = $obj->getChangedFields();
+ $this->assertNotContains('MyMoney', array_keys($changed), 'Field is detected as changed');
+ }
public function testCanOverwriteSettersWithNull()
{
diff --git a/tests/php/ORM/DBMoneyTest/TestObject.php b/tests/php/ORM/DBMoneyTest/TestObject.php
index d21b564..9099971 100644
--- a/tests/php/ORM/DBMoneyTest/TestObject.php
+++ b/tests/php/ORM/DBMoneyTest/TestObject.php
@@ -11,5 +11,6 @@ class TestObject extends DataObject implements TestOnly
private static $db = [
'MyMoney' => 'Money',
+ 'MyText' => 'Text',
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment