diff --git a/Doctrine/Connection.php b/Doctrine/Connection.php index 7ffa0aa9a..51d32b53a 100644 --- a/Doctrine/Connection.php +++ b/Doctrine/Connection.php @@ -832,8 +832,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } $strfields = join(", ", array_keys($array)); - $strvalues = substr(str_repeat("?, ",count($array)),0,-2); - + $strvalues = substr(str_repeat("?, ",count($array)),0,-2); $sql = "INSERT INTO ".$record->getTable()->getTableName()." (".$strfields.") VALUES (".$strvalues.")"; $stmt = $this->dbh->prepare($sql); diff --git a/Doctrine/Hydrate.php b/Doctrine/Hydrate.php index 34dc5a562..8aaa9245d 100644 --- a/Doctrine/Hydrate.php +++ b/Doctrine/Hydrate.php @@ -313,7 +313,6 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { foreach($data as $key => $row) { if(empty($row)) continue; - $ids = $this->tables[$key]->getIdentifier(); @@ -517,8 +516,11 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { foreach($data as $key => $value): $e = explode("__",$key); - $field = array_pop($e); - $component = implode("__",$e); + $field = strtolower( array_pop($e) ); + + $component = strtolower( implode("__",$e) ); + + $data[$component][$field] = $value; unset($data[$key]); diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 7995387e8..5df4a8c25 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -250,7 +250,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @return integer */ private function cleanData() { - $tmp = $this->data; + $tmp = $this->data; $this->data = array(); @@ -259,7 +259,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite foreach($this->table->getColumnNames() as $name) { $type = $this->table->getTypeOf($name); - if( ! isset($tmp[$name])) { + $lower = strtolower($name); + + if( ! isset($tmp[$lower])) { //if($type == 'array') { // $this->data[$name] = array(); //} else @@ -269,23 +271,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite case "array": case "object": - if($tmp[$name] !== self::$null) { - if(is_string($tmp[$name])) { - $value = unserialize($tmp[$name]); + if($tmp[$lower] !== self::$null) { + if(is_string($tmp[$lower])) { + $value = unserialize($tmp[$lower]); if($value === false) - throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$name],true)); + throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$lower],true)); } else - $value = $tmp[$name]; + $value = $tmp[$lower]; $this->data[$name] = $value; } break; case "enum": - $this->data[$name] = $this->table->enumValue($name, $tmp[$name]); + $this->data[$name] = $this->table->enumValue($name, $tmp[$lower]); break; default: - $this->data[$name] = $tmp[$name]; + $this->data[$name] = $tmp[$lower]; endswitch; $count++; } @@ -468,6 +470,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $query = $this->table->getQuery()." WHERE ".implode(" = ? AND ",$this->table->getPrimaryKeys())." = ?"; $this->data = $this->table->getConnection()->execute($query,$id)->fetch(PDO::FETCH_ASSOC); + if( ! $this->data) + throw new Doctrine_Record_Exception('Failed to refresh. Record does not exist anymore'); + + $this->data = array_change_key_case($this->data, CASE_LOWER); + $this->modified = array(); $this->cleanData(); @@ -595,6 +602,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * rawSet * doctrine uses this function internally, not recommended for developers * + * rawSet() works in very same same way as set() with an exception that + * 1. it cannot be used for setting references + * 2. it cannot load uninitialized fields + * * @param mixed $name name of the property or reference * @param mixed $value value of the property or reference */ diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 0e9dd7c62..4a1f71af4 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -63,7 +63,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { */ private $query; /** - * @var Doctrine_Connection $connection Doctrine_Connection object that created this table + * @var Doctrine_Connection $connection Doctrine_Connection object that created this table */ private $connection; /** @@ -128,6 +128,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { + /** * the constructor * @throws Doctrine_ManagerException if there are no opened connections @@ -231,6 +232,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { throw new Doctrine_Exception("Class '$name' has no table definition."); } + $record->setUp(); // save parents @@ -285,6 +287,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $this->primaryKeys[] = $name; } } + /** * @return mixed */ @@ -772,6 +775,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return Doctrine_Record */ public function getRecord() { + $this->data = array_change_key_case($this->data, CASE_LOWER); + $key = $this->getIdentifier(); if( ! is_array($key)) diff --git a/Doctrine/Table/Exception.php b/Doctrine/Table/Exception.php index b5bc52180..f4b29d65a 100644 --- a/Doctrine/Table/Exception.php +++ b/Doctrine/Table/Exception.php @@ -7,7 +7,7 @@ Doctrine::autoload('Doctrine_Exception'); class Doctrine_Table_Exception extends Doctrine_Exception { public function __construct() { parent::__construct("Couldn't initialize table. One instance of this - tabke already exists. Always use Doctrine_Session::getTable(\$name) + table already exists. Always use Doctrine_Session::getTable(\$name) to get on instance of a Doctrine_Table.",Doctrine::ERR_TABLE_INSTANCE); } } diff --git a/tests/RecordTestCase.php b/tests/RecordTestCase.php index 56cf5f851..7e586e0a9 100644 --- a/tests/RecordTestCase.php +++ b/tests/RecordTestCase.php @@ -2,10 +2,12 @@ require_once("UnitTestCase.php"); class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { + public function prepareTables() { $this->tables[] = "enumTest"; parent::prepareTables(); } + public function testReferences2() { $user = new User(); @@ -137,13 +139,40 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $date = new DateTest(); - } + } + public function testEnumType() { + $enum = new EnumTest(); $enum->status = "open"; $this->assertEqual($enum->status, "open"); $enum->save(); $this->assertEqual($enum->status, "open"); + $enum->refresh(); + $this->assertEqual($enum->status, "open"); + + $enum->status = "closed"; + + $this->assertEqual($enum->status, "closed"); + + $enum->save(); + $this->assertEqual($enum->status, "closed"); + + $enum->refresh(); + $this->assertEqual($enum->status, "closed"); + } + + public function testEnumTypeWithCaseConversion() { + $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); + + $enum = new EnumTest(); + + $enum->status = "open"; + $this->assertEqual($enum->status, "open"); + + $enum->save(); + $this->assertEqual($enum->status, "open"); + $enum->refresh(); $this->assertEqual($enum->status, "open"); @@ -157,7 +186,19 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $enum->refresh(); $this->assertEqual($enum->status, "closed"); } - + public function testFailingRefresh() { + $enum = $this->connection->getTable('EnumTest')->find(1); + + $this->dbh->query('DELETE FROM enum_test WHERE id = 1'); + + $f = false; + try { + $enum->refresh(); + } catch(Doctrine_Record_Exception $e) { + $f = true; + } + $this->assertTrue($f); + } public function testSerialize() { $user = $this->connection->getTable("User")->find(4); $str = serialize($user); diff --git a/tests/TableTestCase.php b/tests/TableTestCase.php index c22c2945d..97d221d14 100644 --- a/tests/TableTestCase.php +++ b/tests/TableTestCase.php @@ -1,6 +1,58 @@ tables[] = "FieldNameTest"; + parent::prepareTables(); + } + public function testFieldConversion() { + $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); + + $user = $this->connection->getTable('User')->find(5); + + $this->assertTrue($user instanceof User); + + $t = new FieldNameTest(); + + $t->someColumn = 'abc'; + $t->someEnum = 'php'; + $t->someInt = 1; + $t->someArray = array(); + $obj = new StdClass(); + $t->someObject = $obj; + + $this->assertEqual($t->someColumn, 'abc'); + $this->assertEqual($t->someEnum, 'php'); + $this->assertEqual($t->someInt, 1); + $this->assertEqual($t->someArray, array()); + $this->assertEqual($t->someObject, $obj); + + $t->save(); + + $this->assertEqual($t->someColumn, 'abc'); + $this->assertEqual($t->someEnum, 'php'); + $this->assertEqual($t->someInt, 1); + $this->assertEqual($t->someArray, array()); + $this->assertEqual($t->someObject, $obj); + + $t->refresh(); + + $this->assertEqual($t->someColumn, 'abc'); + $this->assertEqual($t->someEnum, 'php'); + $this->assertEqual($t->someInt, 1); + $this->assertEqual($t->someArray, array()); + $this->assertEqual($t->someObject, $obj); + + $this->connection->clear(); + + $t = $this->connection->getTable('FieldNameTest')->find(1); + + $this->assertEqual($t->someColumn, 'abc'); + $this->assertEqual($t->someEnum, 'php'); + $this->assertEqual($t->someInt, 1); + $this->assertEqual($t->someArray, array()); + $this->assertEqual($t->someObject, $obj); + } public function testBind() { $table = $this->connection->getTable("User"); } @@ -70,8 +122,8 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase { $this->assertEqual($users->count(), 8); $this->assertTrue($users instanceof Doctrine_Collection); } - public function testFindBySql() { - $users = $this->objTable->findBySql("name LIKE '%Arnold%'"); + public function testFindByDql() { + $users = $this->objTable->findByDql("name LIKE '%Arnold%'"); $this->assertEqual($users->count(), 1); $this->assertTrue($users instanceof Doctrine_Collection); } diff --git a/tests/classes.php b/tests/classes.php index a3a350dc7..f2734688c 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -17,6 +17,18 @@ class Entity extends Doctrine_Record { $this->hasColumn("email_id","integer"); } } +class FieldNameTest extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("someColumn", "string", 200); + $this->hasColumn("someEnum", "enum", 4); + $this->hasColumn("someArray", "array", 100); + $this->hasColumn("someObject", "array", 200); + $this->hasColumn("someInt", "integer"); + + + $this->setEnumValues("someEnum", array('php', 'java', 'python')); + } +} class EntityReference extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn("entity1","integer"); diff --git a/tests/run.php b/tests/run.php index 6c1471160..bc4e6de0c 100644 --- a/tests/run.php +++ b/tests/run.php @@ -32,12 +32,12 @@ error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); +$test->addTestCase(new Doctrine_TableTestCase()); + $test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_ConnectionTestCase()); -$test->addTestCase(new Doctrine_TableTestCase()); - $test->addTestCase(new Doctrine_ManagerTestCase()); $test->addTestCase(new Doctrine_AccessTestCase()); @@ -75,6 +75,7 @@ $test->addTestCase(new Doctrine_SchemaTestCase()); $test->addTestCase(new Doctrine_ImportTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase()); + //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());