From ddbf6c949b246b4b0b2f3597cde0d46c5db7b326 Mon Sep 17 00:00:00 2001 From: zYne Date: Sun, 17 Sep 2006 15:44:10 +0000 Subject: [PATCH] Fixed default value support + camelCase column problems --- Doctrine/Locking/Manager/Pessimistic.php | 4 +- Doctrine/Record.php | 53 ++++++++++++------------ Doctrine/Table.php | 19 +++++++-- tests/TableTestCase.php | 9 ++-- 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/Doctrine/Locking/Manager/Pessimistic.php b/Doctrine/Locking/Manager/Pessimistic.php index 4bcda9637..5ecba4927 100644 --- a/Doctrine/Locking/Manager/Pessimistic.php +++ b/Doctrine/Locking/Manager/Pessimistic.php @@ -57,8 +57,8 @@ class Doctrine_Locking_Manager_Pessimistic if($this->_dataSource->getAttribute(Doctrine::ATTR_CREATE_TABLES) === true) { $columns = array(); - $columns['object_type'] = array('string', 50, array('notnull' => true, 'primary')); - $columns['object_key'] = array('string', 250, array('notnull' => true, 'primary')); + $columns['object_type'] = array('string', 50, array('notnull' => true, 'primary' => true)); + $columns['object_key'] = array('string', 250, array('notnull' => true, 'primary' => true)); $columns['user_ident'] = array('string', 50, array('notnull' => true)); $columns['timestamp_obtained'] = array('integer', 10, array('notnull' => true)); diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 6c599e20e..aa411732b 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -253,8 +253,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if($default === null) $default = self::$null; - if($value === self::$null || $overwrite) + if($value === self::$null || $overwrite) { $this->data[$column] = $default; + $this->modified[] = $column; + $this->state = Doctrine_Record::STATE_TDIRTY; + } } } /** @@ -281,35 +284,31 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite foreach($this->table->getColumnNames() as $name) { $type = $this->table->getTypeOf($name); - $lower = strtolower($name); - - if( ! isset($tmp[$lower])) { - //if($type == 'array') { - // $this->data[$name] = array(); - //} else - $this->data[$name] = self::$null; + if( ! isset($tmp[$name])) { + $this->data[$name] = self::$null; } else { switch($type): case "array": case "object": - if($tmp[$lower] !== self::$null) { - if(is_string($tmp[$lower])) { - $value = unserialize($tmp[$lower]); + if($tmp[$name] !== self::$null) { + if(is_string($tmp[$name])) { + $value = unserialize($tmp[$name]); if($value === false) throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$lower],true)); } else - $value = $tmp[$lower]; + $value = $tmp[$name]; $this->data[$name] = $value; } break; case "enum": - $this->data[$name] = $this->table->enumValue($name, $tmp[$lower]); + + $this->data[$name] = $this->table->enumValue($name, $tmp[$name]); break; default: - $this->data[$name] = $tmp[$lower]; + $this->data[$name] = $tmp[$name]; endswitch; $count++; } @@ -601,36 +600,34 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ public function get($name, $invoke = true) { $listener = $this->table->getAttribute(Doctrine::ATTR_LISTENER); - $value = self::$null; + $lower = strtolower($name); - - if(isset($this->data[$name])) { + if(isset($this->data[$lower])) { // check if the property is null (= it is the Doctrine_Null object located in self::$null) - if($this->data[$name] === self::$null) { + if($this->data[$lower] === self::$null) { $this->load(); } - if($this->data[$name] === self::$null) + if($this->data[$lower] === self::$null) $value = null; else - $value = $this->data[$name]; + $value = $this->data[$lower]; } if($value !== self::$null) { if($invoke && $name !== $this->table->getIdentifier()) { - return $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onGetProperty($this, $name, $value); } else return $value; } - if(isset($this->id[$name])) - return $this->id[$name]; + if(isset($this->id[$lower])) + return $this->id[$lower]; if($name === $this->table->getIdentifier()) return null; @@ -706,7 +703,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @return void */ public function set($name,$value) { - if(isset($this->data[$name])) { + $lower = strtolower($name); + + if(isset($this->data[$lower])) { if($value instanceof Doctrine_Record) { $id = $value->getIncremented(); @@ -715,7 +714,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $value = $id; } - $old = $this->get($name, false); + $old = $this->get($lower, false); if($old !== $value) { @@ -725,8 +724,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if($value === null) $value = self::$null; - $this->data[$name] = $value; - $this->modified[] = $name; + $this->data[$lower] = $value; + $this->modified[] = $lower; switch($this->state): case Doctrine_Record::STATE_CLEAN: case Doctrine_Record::STATE_PROXY: diff --git a/Doctrine/Table.php b/Doctrine/Table.php index eec7c0552..13eec89e8 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -291,7 +291,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { unset($options[$k]); } } - + $name = strtolower($name); $this->columns[$name] = array($type,$length,$options); if(isset($options['primary'])) { @@ -318,6 +318,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return mixed */ public function getDefaultValueOf($column) { + $column = strtolower($column); if( ! isset($this->columns[$column])) throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$column." doesn't exist."); @@ -680,7 +681,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $this->relations[$alias] = $relation; return $this->relations[$alias]; } - throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original); + try { + throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original); + } catch(Exception $e) { + print $e; + } } /** * returns an array containing all foreign key objects @@ -922,7 +927,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return void */ final public function setEnumValues($field, array $values) { - $this->enum[$field] = $values; + $this->enum[strtolower($field)] = $values; } /** * @param string $field @@ -936,12 +941,20 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { } /** * enumValue + * + * @param string $field + * @param integer $index + * @return mixed */ final public function enumValue($field, $index) { return isset($this->enum[$field][$index])?$this->enum[$field][$index]:$index; } /** * enumIndex + * + * @param string $field + * @param mixed $value + * @return mixed */ final public function enumIndex($field, $value) { if( ! isset($this->enum[$field])) diff --git a/tests/TableTestCase.php b/tests/TableTestCase.php index 554729146..b647bd3d5 100644 --- a/tests/TableTestCase.php +++ b/tests/TableTestCase.php @@ -5,13 +5,10 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase { $this->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'; @@ -34,7 +31,7 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase { $this->assertEqual($t->someInt, 1); $this->assertEqual($t->someArray, array()); $this->assertEqual($t->someObject, $obj); - + $t->refresh(); $this->assertEqual($t->someColumn, 'abc'); @@ -55,6 +52,7 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase { $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); } + public function testBind() { $table = $this->connection->getTable("User"); } @@ -150,5 +148,6 @@ class Doctrine_TableTestCase extends Doctrine_UnitTestCase { public function testApplyInheritance() { $this->assertEqual($this->objTable->applyInheritance("id = 3"), "id = 3 AND type = ?"); } + } ?>