1
0
mirror of synced 2025-01-18 14:31:40 +03:00

Enum datatype added

This commit is contained in:
doctrine 2006-06-17 22:46:03 +00:00
parent aacb279505
commit 04ca92b094
8 changed files with 104 additions and 9 deletions

View File

@ -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);

View File

@ -82,6 +82,8 @@ class Doctrine_DataDict {
case "bool":
return "L";
break;
case "enum":
case "e":
case "integer":
case "int":
case "i":

View File

@ -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()):

View File

@ -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

View File

@ -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
@ -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
*/

View File

@ -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);

View File

@ -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");