diff --git a/Doctrine/Collection.php b/Doctrine/Collection.php index 2a1315fc2..e6296a3d0 100644 --- a/Doctrine/Collection.php +++ b/Doctrine/Collection.php @@ -312,8 +312,10 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator * @return boolean */ public function remove($key) { - if( ! isset($this->data[$key])) + if( ! isset($this->data[$key])) { + $this->expand($key); throw new InvalidKeyException(); + } $removed = $this->data[$key]; @@ -417,7 +419,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator if(in_array($record,$this->data)) { return false; - } else + } if(isset($this->generator)) { $key = $this->generator->getIndex($record); diff --git a/Doctrine/Form.php b/Doctrine/Form.php index 84a4d7018..464e23a80 100644 --- a/Doctrine/Form.php +++ b/Doctrine/Form.php @@ -21,25 +21,24 @@ class Doctrine_Form implements Iterator { $definitions = $this->columns[$column]; $e = explode("|",$definitions[2]); + + $enum = false; - foreach($e as $v) { - $e2 = explode(":",$v); - if($e2[0] == "enum") { - $enum = explode("-",$e2[1]); - break; - } - } + + if($definitions[0] == "enum") + $enum = $this->record->getTable()->getEnumValues($column); + $length = $definitions[1]; if( ! in_array("autoincrement",$e) && ! in_array("protected",$e)) { if($enum) { $elements[$column] = "\n"; } else { diff --git a/Doctrine/Lib.php b/Doctrine/Lib.php index cee9e4052..cbb9bfd7c 100644 --- a/Doctrine/Lib.php +++ b/Doctrine/Lib.php @@ -29,7 +29,7 @@ class Doctrine_Lib { * @param Doctrine_Record $record * @return string */ - public function getRecordAsString(Doctrine_Record $record) { + public static function getRecordAsString(Doctrine_Record $record) { $r[] = "
"; $r[] = "Component : ".$record->getTable()->getComponentName(); $r[] = "ID : ".$record->getID(); @@ -65,7 +65,7 @@ class Doctrine_Lib { * @param Doctrine_Session $session * @return string */ - public function getSessionAsString(Doctrine_Session $session) { + public static function getSessionAsString(Doctrine_Session $session) { $r[] = ""; $r[] = "Doctrine_Session object"; $r[] = "State : ".Doctrine_Lib::getSessionStateAsString($session->getState()); @@ -109,7 +109,7 @@ class Doctrine_Lib { * @param Doctrine_Table $table * @return string */ - public function getTableAsString(Doctrine_Table $table) { + public static function getTableAsString(Doctrine_Table $table) { $r[] = ""; $r[] = "Component : ".$this->getComponentName(); $r[] = "Table : ".$this->getTableName(); @@ -124,7 +124,7 @@ class Doctrine_Lib { /** * @return string */ - public function formatSql($sql) { + public static function formatSql($sql) { $e = explode("\n",$sql); $color = "367FAC"; $l = $sql; @@ -146,7 +146,7 @@ class Doctrine_Lib { * @param Doctrine_Collection $collection * @return string */ - public function getCollectionAsString(Doctrine_Collection $collection) { + public static function getCollectionAsString(Doctrine_Collection $collection) { $r[] = ""; $r[] = get_class($collection); diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 9e605df20..465172d50 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -172,6 +172,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite // listen the onLoad event $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this); } + $this->table->getRepository()->add($this); } } @@ -229,7 +230,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if( ! isset($tmp[$name])) { if($type == 'array') { $this->data[$name] = array(); - $this->modified[] = $name; } else $this->data[$name] = self::$null; } else { @@ -715,7 +715,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite final public function getModified() { $a = array(); - foreach($this->modified as $k=>$v) { + foreach($this->modified as $k => $v) { $a[$v] = $this->data[$v]; } return $a; @@ -726,10 +726,13 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * * @return array */ - final public function getPrepared() { + final public function getPrepared(array $array = array()) { $a = array(); - foreach($this->modified as $k => $v) { + if(empty($array)) + $array = $this->modified; + + foreach($array as $k => $v) { $type = $this->table->getTypeOf($v); if($type == 'array' || diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 4027b1105..c0da3d237 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -127,12 +127,9 @@ class Doctrine_Table extends Doctrine_Configurable { $record = new $name($this); - $names = array(); $class = $name; - - // get parent classes @@ -825,6 +822,16 @@ class Doctrine_Table extends Doctrine_Configurable { final public function setEnumValues($field, array $values) { $this->enum[$field] = $values; } + /** + * @param string $field + * @return array + */ + final public function getEnumValues($field) { + if(isset($this->enum[$field])) + return $this->enum[$field]; + else + return array(); + } /** * enumValue */ @@ -836,7 +843,7 @@ class Doctrine_Table extends Doctrine_Configurable { */ final public function enumIndex($field, $value) { $v = array_search($value, $this->enum[$field]); - return ($v !== false)?$v:$value; + return $v; } /** * @return integer diff --git a/Doctrine/Validator.php b/Doctrine/Validator.php index 9df5d88dc..5385e88ab 100644 --- a/Doctrine/Validator.php +++ b/Doctrine/Validator.php @@ -106,21 +106,31 @@ class Doctrine_Validator { switch($record->getState()): case Doctrine_Record::STATE_TDIRTY: case Doctrine_Record::STATE_TCLEAN: + // all fields will be validated $data = $record->getData(); break; default: + // only the modified fields will be validated $data = $record->getModified(); endswitch; $err = array(); - foreach($data as $key => $value) { if($value === self::$null) $value = null; $column = $columns[$key]; + + if($column[0] == "enum") { + $value = $record->getTable()->enumIndex($value); - if($column[0] == 'array' || $column[0] == 'object') + if($value === false) { + $err[$key] = Doctrine_Validator::ERR_ENUM; + continue; + } + } + + if($column[0] == "array" || $column[0] == "object") $length = strlen(serialize($value)); else $length = strlen($value); @@ -199,6 +209,9 @@ class Doctrine_Validator { */ public static function isValidType($var, $type) { $looseType = self::gettype($var); + if($type == 'enum') + $type = 'integer'; + switch($looseType): case 'float': case 'double': diff --git a/tests/ValidatorTestCase.php b/tests/ValidatorTestCase.php index 47c071967..996b9eb35 100644 --- a/tests/ValidatorTestCase.php +++ b/tests/ValidatorTestCase.php @@ -158,5 +158,6 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { $this->assertEqual($a["User"][0]["name"], Doctrine_Validator::ERR_LENGTH); $this->manager->setAttribute(Doctrine::ATTR_VLD, false); } + } ?>