Commit bf5deba9 authored by doctrine's avatar doctrine

New datatypes: array and object

parent 54801dde
...@@ -80,6 +80,7 @@ class Doctrine_DB extends PDO implements Countable, IteratorAggregate { ...@@ -80,6 +80,7 @@ class Doctrine_DB extends PDO implements Countable, IteratorAggregate {
*/ */
public function prepare($query) { public function prepare($query) {
$this->queries[] = $query; $this->queries[] = $query;
return parent::prepare($query); return parent::prepare($query);
} }
/** /**
......
...@@ -44,6 +44,10 @@ class Doctrine_DataDict { ...@@ -44,6 +44,10 @@ class Doctrine_DataDict {
*/ */
public function getADOType($type,$length) { public function getADOType($type,$length) {
switch($type): switch($type):
case "array":
case "a":
case "object":
case "o":
case "string": case "string":
case "s": case "s":
if($length < 255) if($length < 255)
......
...@@ -40,6 +40,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -40,6 +40,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
Doctrine_Record::initNullObject($this->null); Doctrine_Record::initNullObject($this->null);
Doctrine_Collection::initNullObject($this->null); Doctrine_Collection::initNullObject($this->null);
Doctrine_Record_Iterator::initNullObject($this->null);
} }
/** /**
* @return Doctrine_Null * @return Doctrine_Null
......
...@@ -57,16 +57,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -57,16 +57,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @var array $data the record data * @var array $data the record data
*/ */
protected $data = array(); 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 * @var integer $state the state of this record
* @see STATE_* constants * @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 * @var array $collections the collections this record is in
*/ */
...@@ -134,13 +133,13 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -134,13 +133,13 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$count = count($this->data); $count = count($this->data);
// clean data array // clean data array
$cols = $this->cleanData(); $this->cleanData();
$this->prepareIdentifiers($exists); $this->prepareIdentifiers($exists);
if( ! $exists) { if( ! $exists) {
if($cols > 0) if($count > 0)
$this->state = Doctrine_Record::STATE_TDIRTY; $this->state = Doctrine_Record::STATE_TDIRTY;
else else
$this->state = Doctrine_Record::STATE_TCLEAN; $this->state = Doctrine_Record::STATE_TCLEAN;
...@@ -164,10 +163,18 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -164,10 +163,18 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
/** /**
* initNullObject * initNullObject
*
* @param Doctrine_Null $null
*/ */
public static function initNullObject(Doctrine_Null $null) { public static function initNullObject(Doctrine_Null $null) {
self::$null = $null; self::$null = $null;
} }
/**
* @return Doctrine_Null
*/
public static function getNullObject() {
return self::$null;
}
/** /**
* setUp * setUp
* implemented by child classes * implemented by child classes
...@@ -192,7 +199,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -192,7 +199,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* $data = array("name"=>"John","lastname" => array(),"id"=>1); * $data = array("name"=>"John","lastname" => array(),"id"=>1);
*/ */
private function cleanData() { private function cleanData() {
$cols = 0;
$tmp = $this->data; $tmp = $this->data;
$this->data = array(); $this->data = array();
...@@ -200,14 +206,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -200,14 +206,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
foreach($this->table->getColumnNames() as $name) { foreach($this->table->getColumnNames() as $name) {
if( ! isset($tmp[$name])) { if( ! isset($tmp[$name])) {
$this->data[$name] = self::$null; $this->data[$name] = self::$null;
} else { } else {
$cols++;
$this->data[$name] = $tmp[$name]; $this->data[$name] = $tmp[$name];
} }
} }
return $cols;
} }
/** /**
* prepares identifiers * prepares identifiers
...@@ -251,9 +253,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -251,9 +253,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->table = $this->table->getComponentName(); $this->table = $this->table->getComponentName();
// unset all vars that won't need to be serialized // unset all vars that won't need to be serialized
unset($this->modified);
unset($this->associations); unset($this->associations);
unset($this->state);
unset($this->collections); unset($this->collections);
unset($this->references); unset($this->references);
unset($this->originals); unset($this->originals);
...@@ -262,6 +262,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -262,6 +262,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
foreach($this->data as $k=>$v) { foreach($this->data as $k=>$v) {
if($v instanceof Doctrine_Record) if($v instanceof Doctrine_Record)
$this->data[$k] = array(); $this->data[$k] = array();
elseif($v === self::$null)
unset($this->data[$k]);
} }
return array_keys(get_object_vars($this)); return array_keys(get_object_vars($this));
...@@ -273,10 +275,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -273,10 +275,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return void * @return void
*/ */
public function __wakeup() { public function __wakeup() {
$this->modified = array();
$this->state = Doctrine_Record::STATE_CLEAN;
$name = $this->table; $name = $this->table;
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
...@@ -405,6 +403,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -405,6 +403,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
final public function getData() { final public function getData() {
return $this->data; 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 * get
* returns a value of a property or a related component * returns a value of a property or a related component
...@@ -658,10 +673,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -658,10 +673,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
/** /**
* getIterator * 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() { public function getIterator() {
return new ArrayIterator($this->data); return new Doctrine_Record_Iterator($this);
} }
/** /**
* saveAssociations * saveAssociations
......
<?php
class Doctrine_Record_Iterator extends ArrayIterator {
/**
* @var Doctrine_Record $record
*/
private $record;
/**
* @var Doctrine_Null $null
*/
private static $null;
/**
* constructor
*
* @param Doctrine_Record $record
*/
public function __construct(Doctrine_Record $record) {
$this->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;
}
}
?>
...@@ -65,7 +65,9 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -65,7 +65,9 @@ class Doctrine_Table extends Doctrine_Configurable {
* @var array $identityMap first level cache * @var array $identityMap first level cache
*/ */
private $identityMap = array(); private $identityMap = array();
/**
* @var Doctrine_Repository $repository record repository
*/
private $repository; private $repository;
/** /**
...@@ -717,11 +719,17 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -717,11 +719,17 @@ class Doctrine_Table extends Doctrine_Configurable {
$id[] = $this->data[$k]; $id[] = $this->data[$k];
} }
$id = implode(' ', $id); $id = implode(' ', $id);
if(isset($this->identityMap[$id])) if(isset($this->identityMap[$id]))
$record = $this->identityMap[$id]; $record = $this->identityMap[$id];
else { else {
/**
if($this->createsChildren) {
}
*/
$record = new $this->name($this); $record = new $this->name($this);
$this->identityMap[$id] = $record; $this->identityMap[$id] = $record;
} }
......
...@@ -22,6 +22,12 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -22,6 +22,12 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users[0]->Email->address, 'zYne@example.com'); $this->assertEqual($users[0]->Email->address, 'zYne@example.com');
$this->assertEqual(($count + 1),$this->dbh->count()); $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() { public function testLazyPropertyFetchingWithMultipleColumns() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment