From bf5deba9fdd794b7e54bf30556ad3ea570f40bbd Mon Sep 17 00:00:00 2001 From: doctrine Date: Thu, 1 Jun 2006 11:58:05 +0000 Subject: [PATCH] New datatypes: array and object --- Doctrine/DB.php | 1 + Doctrine/DataDict.php | 4 +++ Doctrine/Manager.php | 1 + Doctrine/Record.php | 65 +++++++++++++++++++++-------------- Doctrine/Record/Iterator.php | 42 ++++++++++++++++++++++ Doctrine/Table.php | 12 +++++-- tests/QueryTestCase.class.php | 6 ++++ 7 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 Doctrine/Record/Iterator.php diff --git a/Doctrine/DB.php b/Doctrine/DB.php index ab82c3876..46809dfd4 100644 --- a/Doctrine/DB.php +++ b/Doctrine/DB.php @@ -80,6 +80,7 @@ class Doctrine_DB extends PDO implements Countable, IteratorAggregate { */ public function prepare($query) { $this->queries[] = $query; + return parent::prepare($query); } /** diff --git a/Doctrine/DataDict.php b/Doctrine/DataDict.php index f7d5f773b..13e7a8bc5 100644 --- a/Doctrine/DataDict.php +++ b/Doctrine/DataDict.php @@ -44,6 +44,10 @@ class Doctrine_DataDict { */ public function getADOType($type,$length) { switch($type): + case "array": + case "a": + case "object": + case "o": case "string": case "s": if($length < 255) diff --git a/Doctrine/Manager.php b/Doctrine/Manager.php index 09c5e50ad..4e27cf8da 100644 --- a/Doctrine/Manager.php +++ b/Doctrine/Manager.php @@ -40,6 +40,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera Doctrine_Record::initNullObject($this->null); Doctrine_Collection::initNullObject($this->null); + Doctrine_Record_Iterator::initNullObject($this->null); } /** * @return Doctrine_Null diff --git a/Doctrine/Record.php b/Doctrine/Record.php index e1f934627..7e421f612 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -57,16 +57,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @var array $data the record data */ protected $data = array(); - - /** - * @var array $modified an array containing properties that have been modified - */ - private $modified = array(); /** * @var integer $state the state of this record * @see STATE_* constants */ - private $state; + protected $state; + /** + * @var array $modified an array containing properties that have been modified + */ + protected $modified = array(); /** * @var array $collections the collections this record is in */ @@ -134,13 +133,13 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $count = count($this->data); // clean data array - $cols = $this->cleanData(); + $this->cleanData(); $this->prepareIdentifiers($exists); if( ! $exists) { - if($cols > 0) + if($count > 0) $this->state = Doctrine_Record::STATE_TDIRTY; else $this->state = Doctrine_Record::STATE_TCLEAN; @@ -164,10 +163,18 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } /** * initNullObject + * + * @param Doctrine_Null $null */ public static function initNullObject(Doctrine_Null $null) { self::$null = $null; } + /** + * @return Doctrine_Null + */ + public static function getNullObject() { + return self::$null; + } /** * setUp * implemented by child classes @@ -192,22 +199,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * $data = array("name"=>"John","lastname" => array(),"id"=>1); */ private function cleanData() { - $cols = 0; $tmp = $this->data; - + $this->data = array(); foreach($this->table->getColumnNames() as $name) { if( ! isset($tmp[$name])) { $this->data[$name] = self::$null; - } else { - $cols++; $this->data[$name] = $tmp[$name]; } } - - return $cols; } /** * prepares identifiers @@ -251,9 +253,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->table = $this->table->getComponentName(); // unset all vars that won't need to be serialized - unset($this->modified); unset($this->associations); - unset($this->state); unset($this->collections); unset($this->references); unset($this->originals); @@ -262,6 +262,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite foreach($this->data as $k=>$v) { if($v instanceof Doctrine_Record) $this->data[$k] = array(); + elseif($v === self::$null) + unset($this->data[$k]); } return array_keys(get_object_vars($this)); @@ -273,10 +275,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @return void */ public function __wakeup() { - - $this->modified = array(); - $this->state = Doctrine_Record::STATE_CLEAN; - $name = $this->table; $manager = Doctrine_Manager::getInstance(); @@ -405,6 +403,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite final public function getData() { return $this->data; } + /** + * rawGet + * returns the value of a property, if the property is not yet loaded + * this method does NOT load it + * + * @param $name name of the property + * @return mixed + */ + public function rawGet($name) { + if( ! isset($this->data[$name])) + throw new InvalidKeyException(); + + if($this->data[$name] == self::$null) + return null; + + return $this->data[$name]; + } /** * get * returns a value of a property or a related component @@ -459,7 +474,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if( ! empty($id)) $value = $id; - + if(isset($this->data[$name])) { if($this->data[$name] === self::$null) { if($this->data[$name] !== $value) { @@ -477,7 +492,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->state = Doctrine_Record::STATE_TDIRTY; $this->data[$name] = $value; - $this->modified[] = $name; + $this->modified[] = $name; } } /** @@ -658,10 +673,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } /** * getIterator - * @return ArrayIterator an ArrayIterator that iterates through the data + * @return Doctrine_Record_Iterator a Doctrine_Record_Iterator that iterates through the data */ public function getIterator() { - return new ArrayIterator($this->data); + return new Doctrine_Record_Iterator($this); } /** * saveAssociations @@ -1112,7 +1127,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite public function __call($m,$a) { if( ! function_exists($m)) throw new Doctrine_Record_Exception("unknown callback '$m'"); - + if(isset($a[0])) { $column = $a[0]; $a[0] = $this->get($column); diff --git a/Doctrine/Record/Iterator.php b/Doctrine/Record/Iterator.php new file mode 100644 index 000000000..f0ff507a7 --- /dev/null +++ b/Doctrine/Record/Iterator.php @@ -0,0 +1,42 @@ +record = $record; + parent::__construct($record->getData()); + } + /** + * initNullObject + * + * @param Doctrine_Null $null + */ + public static function initNullObject(Doctrine_Null $null) { + self::$null = $null; + } + /** + * current + * + * @return mixed + */ + public function current() { + $value = parent::current(); + + if($value == self::$null) + return null; + else + return $value; + } +} +?> diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 440f5281e..63d9e2e25 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -65,7 +65,9 @@ class Doctrine_Table extends Doctrine_Configurable { * @var array $identityMap first level cache */ private $identityMap = array(); - + /** + * @var Doctrine_Repository $repository record repository + */ private $repository; /** @@ -714,14 +716,20 @@ class Doctrine_Table extends Doctrine_Configurable { foreach($key as $k) { if( ! isset($this->data[$k])) throw new Doctrine_Exception("No primary key found"); - + $id[] = $this->data[$k]; } + $id = implode(' ', $id); if(isset($this->identityMap[$id])) $record = $this->identityMap[$id]; else { + /** + if($this->createsChildren) { + + } + */ $record = new $this->name($this); $this->identityMap[$id] = $record; } diff --git a/tests/QueryTestCase.class.php b/tests/QueryTestCase.class.php index f55d730bb..27c57378a 100644 --- a/tests/QueryTestCase.class.php +++ b/tests/QueryTestCase.class.php @@ -22,6 +22,12 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[0]->Email->address, 'zYne@example.com'); $this->assertEqual(($count + 1),$this->dbh->count()); + + $this->assertEqual(get_class($users[1]->Email), 'Email'); + $this->assertEqual(($count + 1),$this->dbh->count()); + + $this->assertEqual($users[1]->Email->address, 'arnold@example.com'); + $this->assertEqual(($count + 1),$this->dbh->count()); } public function testLazyPropertyFetchingWithMultipleColumns() {