diff --git a/Doctrine/Collection.php b/Doctrine/Collection.php index a69b2d085..2a1315fc2 100644 --- a/Doctrine/Collection.php +++ b/Doctrine/Collection.php @@ -428,6 +428,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator return true; } /** + * populate + * * @param Doctrine_Query $query * @param integer $key */ @@ -458,6 +460,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator } } /** + * getNormalIterator + * returns normal iterator - an iterator that will not expand this collection + * * @return Doctrine_Iterator_Normal */ public function getNormalIterator() { @@ -466,6 +471,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator /** * save * saves all records + * + * @return void */ public function save() { $this->table->getSession()->saveCollection($this); diff --git a/Doctrine/DataDict.php b/Doctrine/DataDict.php index 13e7a8bc5..cea676062 100644 --- a/Doctrine/DataDict.php +++ b/Doctrine/DataDict.php @@ -82,6 +82,8 @@ class Doctrine_DataDict { case "bool": return "L"; break; + case "enum": + case "e": case "integer": case "int": case "i": diff --git a/Doctrine/Module.class.php b/Doctrine/Module.php similarity index 100% rename from Doctrine/Module.class.php rename to Doctrine/Module.php diff --git a/Doctrine/Query.php b/Doctrine/Query.php index ac2901fb4..4cd775b59 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -386,7 +386,7 @@ class Doctrine_Query extends Doctrine_Access { foreach($maps as $map) { $b = array(); foreach($map as $field => $value) { - $b[] = $tname.".$field = $value"; + $b[] = $tname.".$field = $value"; //OR $tname.$field IS NULL"; } if( ! empty($b)) $a[] = implode(" AND ",$b); } @@ -526,9 +526,13 @@ class Doctrine_Query extends Doctrine_Access { if($emptyID) { + $pointer = $this->joins[$name]; $alias = $this->tables[$pointer]->getAlias($name); $fk = $this->tables[$pointer]->getForeignKey($alias); + if( ! $prev[$pointer]) + continue; + $last = $prev[$pointer]->getLast(); switch($fk->getType()): diff --git a/Doctrine/Record.php b/Doctrine/Record.php index b98e5c082..9e605df20 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -213,6 +213,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * $data = array("name"=>"John","lastname" => Object(Doctrine_Null)); * * here column 'id' is removed since its auto-incremented primary key (protected) + * + * @return integer */ private function cleanData() { $tmp = $this->data; @@ -236,12 +238,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite case "object": if($tmp[$name] !== self::$null) $this->data[$name] = unserialize($tmp[$name]); - + break; + case "enum": + $this->data[$name] = $this->table->enumValue($name, $tmp[$name]); break; default: $this->data[$name] = $tmp[$name]; - $count++; endswitch; + $count++; } } return $count; @@ -429,6 +433,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } /** * factoryRefresh + * refreshes the data from outer source (Doctrine_Table) + * * @throws Doctrine_Exception * @return void */ @@ -731,6 +737,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $a[$v] = serialize($this->data[$v]); continue; + + } elseif($type == 'enum') { + $a[$v] = $this->table->enumIndex($v,$this->data[$v]); + continue; } if($this->data[$v] instanceof Doctrine_Record) @@ -1127,6 +1137,16 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite break; endswitch; } + /** + * sets enumerated value array for given field + * + * @param string $field + * @param array $values + * @return void + */ + final public function setEnumValues($field, array $values) { + $this->table->setEnumValues($field, $values); + } /** * binds One-to-One composite relation diff --git a/Doctrine/Table.php b/Doctrine/Table.php index a6dbda2f3..4027b1105 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -102,6 +102,12 @@ class Doctrine_Table extends Doctrine_Configurable { * @var array $parents the parent classes of this component */ private $parents = array(); + /** + * @var array $enum enum value arrays + */ + private $enum = array(); + + /** * the constructor @@ -328,7 +334,7 @@ class Doctrine_Table extends Doctrine_Configurable { * getParents */ final public function getParents() { - return $this->parents; + return $this->parents; } /** * @return boolean @@ -742,11 +748,6 @@ class Doctrine_Table extends Doctrine_Configurable { if(isset($this->identityMap[$id])) $record = $this->identityMap[$id]; else { - /** - if($this->createsChildren) { - - } - */ $record = new $this->name($this); $this->identityMap[$id] = $record; } @@ -814,12 +815,36 @@ class Doctrine_Table extends Doctrine_Configurable { } return $coll; } + /** + * sets enumerated value array for given field + * + * @param string $field + * @param array $values + * @return void + */ + final public function setEnumValues($field, array $values) { + $this->enum[$field] = $values; + } + /** + * enumValue + */ + final public function enumValue($field, $index) { + return isset($this->enum[$field][$index])?$this->enum[$field][$index]:$index; + } + /** + * enumIndex + */ + final public function enumIndex($field, $value) { + $v = array_search($value, $this->enum[$field]); + return ($v !== false)?$v:$value; + } /** * @return integer */ final public function getColumnCount() { return $this->columnCount; } + /** * returns all columns and their definitions * @@ -836,6 +861,13 @@ class Doctrine_Table extends Doctrine_Configurable { public function getColumnNames() { return array_keys($this->columns); } + /** + * getDefinitionOf + */ + public function getDefinitionOf($column) { + if(isset($this->columns[$column])) + return $this->columns[$column]; + } /** * getTypeOf */ diff --git a/tests/RecordTestCase.php b/tests/RecordTestCase.php index 43bcbc01e..c4a650af0 100644 --- a/tests/RecordTestCase.php +++ b/tests/RecordTestCase.php @@ -2,6 +2,30 @@ require_once("UnitTestCase.php"); class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { + public function prepareTables() { + $this->tables[] = "enumTest"; + parent::prepareTables(); + } + 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 testSerialize() { $user = $this->session->getTable("User")->find(4); $str = serialize($user); diff --git a/tests/classes.php b/tests/classes.php index e79a4026c..3802d0908 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -312,6 +312,12 @@ class ORM_AccessControlsGroups extends Doctrine_Record { $this->setPrimaryKey(array("accessControlID", "accessGroupID")); } } +class EnumTest extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("status","enum",11); + $this->setEnumValues("status", array("open","verified","closed")); + } +} class Log_Entry extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn("stamp", "timestamp");