diff --git a/lib/Doctrine/Db/Statement.php b/lib/Doctrine/Db/Statement.php index 3b61543da..5e0ce2d67 100644 --- a/lib/Doctrine/Db/Statement.php +++ b/lib/Doctrine/Db/Statement.php @@ -42,6 +42,10 @@ class Doctrine_Db_Statement implements Doctrine_Adapter_Statement_Interface { $this->adapter = $adapter; $this->stmt = $stmt; + + if ($stmt === false) { + throw new Doctrine_Db_Exception('Unknown statement object given.'); + } } /** * diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index aff651f0a..dc3abfab6 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -382,7 +382,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * * @return integer */ - private function cleanData($debug = false) + private function cleanData() { $tmp = $this->_data; @@ -397,42 +397,57 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->_data[$name] = self::$null; } else { switch ($type) { - case "array": - case "object": + case 'array': + case 'object': 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."); + throw new Doctrine_Record_Exception('Unserialization of ' . $name . ' failed.'); } else { $value = $tmp[$name]; } $this->_data[$name] = $value; } break; - case "gzip": + case 'gzip': if ($tmp[$name] !== self::$null) { $value = gzuncompress($tmp[$name]); if ($value === false) - throw new Doctrine_Record_Exception("Uncompressing of $name failed."); + throw new Doctrine_Record_Exception('Uncompressing of ' . $name . ' failed.'); $this->_data[$name] = $value; } break; - case "enum": + case 'enum': $this->_data[$name] = $this->_table->enumValue($name, $tmp[$name]); break; default: $this->_data[$name] = $tmp[$name]; - }; + } $count++; } } return $count; } + /** + * hydrate + * hydrates this object from given array + * + * @param array $data + * @return boolean + */ + public function hydrate(array $data) + { + foreach ($data as $k => $v) { + $this->_data[$k] = $v; + } + $this->cleanData(); + $this->prepareIdentifiers(); + } /** * prepareIdentifiers * prepares identifiers for later use diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 9c48c357b..2f7aeed90 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -1144,6 +1144,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable if (isset($this->identityMap[$id])) { $record = $this->identityMap[$id]; + $record->hydrate($this->data); } else { $recordName = $this->getClassnameToReturn(); $record = new $recordName($this); @@ -1183,7 +1184,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable break; } } - if (!$nomatch) { + if ( ! $nomatch) { return $table->getComponentName(); } } diff --git a/tests/Record/StateTestCase.php b/tests/Record/StateTestCase.php index f73fa6dcf..b2158644c 100644 --- a/tests/Record/StateTestCase.php +++ b/tests/Record/StateTestCase.php @@ -107,4 +107,27 @@ class Doctrine_Record_State_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($user->state(), Doctrine_Record::STATE_PROXY); } + public function testProxiesAreAutomaticallyUpdatedWithFetches() + { + $user = new User(); + $user->name = 'someuser'; + $user->password = '123'; + $user->save(); + + $this->connection->clear(); + + $user = $this->connection->queryOne("SELECT u.name FROM User u WHERE u.name = 'someuser'"); + + $this->assertEqual($user->state(), Doctrine_Record::STATE_PROXY); + + $user2 = $this->connection->queryOne("FROM User u WHERE u.name = 'someuser'"); + + $this->assertEqual($user->getOID(), $user2->getOID()); + + $count = count($this->dbh); + + $this->assertEqual($user->password, '123'); + + $this->assertEqual($count, count($this->dbh)); + } }