1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Added exception info in Doctrine_Record doc blocks, fixes #125

Ticket: 125
This commit is contained in:
zYne 2006-09-27 18:02:13 +00:00
parent bed3a3712d
commit c239ff4bba
3 changed files with 82 additions and 48 deletions

View File

@ -85,7 +85,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
Doctrine::ATTR_FETCHMODE => Doctrine::FETCH_IMMEDIATE, Doctrine::ATTR_FETCHMODE => Doctrine::FETCH_IMMEDIATE,
Doctrine::ATTR_BATCH_SIZE => 5, Doctrine::ATTR_BATCH_SIZE => 5,
Doctrine::ATTR_COLL_LIMIT => 5, Doctrine::ATTR_COLL_LIMIT => 5,
Doctrine::ATTR_LISTENER => new Doctrine_EventListener_Empty(), Doctrine::ATTR_LISTENER => new Doctrine_EventListener(),
Doctrine::ATTR_LOCKMODE => 1, Doctrine::ATTR_LOCKMODE => 1,
Doctrine::ATTR_VLD => false, Doctrine::ATTR_VLD => false,
Doctrine::ATTR_CREATE_TABLES => true, Doctrine::ATTR_CREATE_TABLES => true,

View File

@ -138,9 +138,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
/** /**
* constructor * constructor
* @param Doctrine_Table $table a Doctrine_Table object * @param Doctrine_Table|null $table a Doctrine_Table object or null,
* if null the table object is retrieved from current connection
*
* @throws Doctrine_Connection_Exception if object is created using the new operator and there are no * @throws Doctrine_Connection_Exception if object is created using the new operator and there are no
* open connections * open connections
* @throws Doctrine_Record_Exception if the cleanData operation fails somehow
*/ */
public function __construct($table = null) { public function __construct($table = null) {
if(isset($table) && $table instanceof Doctrine_Table) { if(isset($table) && $table instanceof Doctrine_Table) {
@ -235,7 +238,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public function setUp() { } public function setUp() { }
/** /**
* getOID * getOID
* return the object identifier * returns the object identifier
* *
* @return integer * @return integer
*/ */
@ -244,7 +247,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
/** /**
* setDefaultValues * setDefaultValues
* sets the default values * sets the default values for records internal data
* *
* @param boolean $overwrite whether or not to overwrite the already set values * @param boolean $overwrite whether or not to overwrite the already set values
* @return boolean * @return boolean
@ -268,7 +271,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
/** /**
* cleanData * cleanData
* modifies data array * this method does several things to records internal data
*
* 1. It unserializes array and object typed columns
* 2. Uncompresses gzip typed columns
* 3. Gets the appropriate enum values for enum typed columns
* 4. Initializes special null object pointer for null values (for fast column existence checking purposes)
*
*
* example: * example:
* *
* $data = array("name"=>"John","lastname"=> null, "id" => 1,"unknown" => "unknown"); * $data = array("name"=>"John","lastname"=> null, "id" => 1,"unknown" => "unknown");
@ -276,7 +286,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* $data after operation: * $data after operation:
* $data = array("name"=>"John","lastname" => Object(Doctrine_Null)); * $data = array("name"=>"John","lastname" => Object(Doctrine_Null));
* *
* here column 'id' is removed since its auto-incremented primary key (protected) * here column 'id' is removed since its auto-incremented primary key (read-only)
*
* @throws Doctrine_Record_Exception if unserialization of array/object typed column fails or
* if uncompression of gzip typed column fails
* *
* @return integer * @return integer
*/ */
@ -302,7 +315,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$value = unserialize($tmp[$name]); $value = unserialize($tmp[$name]);
if($value === false) if($value === false)
throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump($tmp[$lower],true)); throw new Doctrine_Record_Exception("Unserialization of $name failed. ".var_dump(substr($tmp[$lower],0,30)."...",true));
} else } else
$value = $tmp[$name]; $value = $tmp[$name];
@ -324,10 +337,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
case "enum": case "enum":
$this->data[$name] = $this->table->enumValue($name, $tmp[$name]); $this->data[$name] = $this->table->enumValue($name, $tmp[$name]);
break; break;
case "boolean":
case "integer":
if($tmp[$name] !== self::$null)
settype($tmp[$name], $type);
default: default:
$this->data[$name] = $tmp[$name]; $this->data[$name] = $tmp[$name];
endswitch; endswitch;
@ -338,7 +347,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $count; return $count;
} }
/** /**
* prepareIdentifiers
* prepares identifiers for later use * prepares identifiers for later use
* *
* @param boolean $exists whether or not this record exists in persistent data store * @param boolean $exists whether or not this record exists in persistent data store
@ -379,6 +389,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
endswitch; endswitch;
} }
/** /**
* serialize
* this method is automatically called when this Doctrine_Record is serialized * this method is automatically called when this Doctrine_Record is serialized
* *
* @return array * @return array
@ -418,6 +429,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* unseralize * unseralize
* this method is automatically called everytime a Doctrine_Record object is unserialized * this method is automatically called everytime a Doctrine_Record object is unserialized
* *
* @param string $serialized Doctrine_Record as serialized string
* @throws Doctrine_Record_Exception if the cleanData operation fails somehow
* @return void * @return void
*/ */
public function unserialize($serialized) { public function unserialize($serialized) {
@ -454,14 +467,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
/** /**
* addCollection * addCollection
*
* @param Doctrine_Collection $collection * @param Doctrine_Collection $collection
* @param mixed $key * @param mixed $key
*/ */
final public function addCollection(Doctrine_Collection $collection,$key = null) { final public function addCollection(Doctrine_Collection $collection,$key = null) {
if($key !== null) { if($key !== null) {
if(isset($this->collections[$key]))
throw InvalidKeyException();
$this->collections[$key] = $collection; $this->collections[$key] = $collection;
} else { } else {
$this->collections[] = $collection; $this->collections[] = $collection;
@ -498,6 +509,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* refresh * refresh
* refresh internal data from the database * refresh internal data from the database
* *
* @throws Doctrine_Record_Exception When the refresh operation fails (when the database row
* this record represents does not exist anymore)
* @return boolean * @return boolean
*/ */
final public function refresh() { final public function refresh() {
@ -536,7 +549,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* factoryRefresh * factoryRefresh
* refreshes the data from outer source (Doctrine_Table) * refreshes the data from outer source (Doctrine_Table)
* *
* @throws Doctrine_Exception * @throws Doctrine_Record_Exception When the primary key of this record doesn't match the primary key fetched from a collection
* @return void * @return void
*/ */
final public function factoryRefresh() { final public function factoryRefresh() {
@ -556,15 +569,19 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this); $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this);
} }
/** /**
* return the factory that created this data access object * getTable
* returns the table object for this record
*
* @return object Doctrine_Table a Doctrine_Table object * @return object Doctrine_Table a Doctrine_Table object
*/ */
final public function getTable() { final public function getTable() {
return $this->table; return $this->table;
} }
/** /**
* getData
* return all the internal data * return all the internal data
* @return array an array containing all the properties *
* @return array an array containing all the properties
*/ */
final public function getData() { final public function getData() {
return $this->data; return $this->data;
@ -617,7 +634,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* *
* @param mixed $name name of the property or related component * @param mixed $name name of the property or related component
* @param boolean $invoke whether or not to invoke the onGetProperty listener * @param boolean $invoke whether or not to invoke the onGetProperty listener
* @throws Doctrine_Exception * @throws Doctrine_Record_Exception if trying to get a value of unknown property / related component
* @return mixed * @return mixed
*/ */
public function get($name, $invoke = true) { public function get($name, $invoke = true) {
@ -654,9 +671,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if($name === $this->table->getIdentifier()) if($name === $this->table->getIdentifier())
return null; return null;
if( ! isset($this->references[$name])) $rel = $this->table->getRelation($name);
$this->loadReference($name);
try {
if( ! isset($this->references[$name]))
$this->loadReference($name);
} catch(Doctrine_Table_Exception $e) {
throw new Doctrine_Record_Exception("Unknown property / related component '$name'.");
}
return $this->references[$name]; return $this->references[$name];
} }
@ -684,6 +706,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @param mixed $value value of the property or reference * @param mixed $value value of the property or reference
*/ */
final public function rawSet($name,$value) { final public function rawSet($name,$value) {
$name = strtolower($name);
if($value instanceof Doctrine_Record) if($value instanceof Doctrine_Record)
$id = $value->getIncremented(); $id = $value->getIncremented();
@ -718,11 +742,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* set * set
* method for altering properties and Doctrine_Record references * method for altering properties and Doctrine_Record references
* *
* @param mixed $name name of the property or reference * @param mixed $name name of the property or reference
* @param mixed $value value of the property or reference * @param mixed $value value of the property or reference
* @throws InvalidKeyException * @throws Doctrine_Record_Exception if trying to set a value for unknown property / related component
* @throws InvalidTypeException * @return Doctrine_Record
* @return void
*/ */
public function set($name,$value) { public function set($name,$value) {
$lower = strtolower($name); $lower = strtolower($name);
@ -759,21 +782,22 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
endswitch; endswitch;
} }
} else { } else {
// if not found, throws InvalidKeyException try {
$rel = $this->table->getRelation($name);
$fk = $this->table->getRelation($name); } catch(Doctrine_Table_Exception $e) {
throw new Doctrine_Record_Exception("Unknown property / related component '$name'.");
}
// one-to-many or one-to-one relation // one-to-many or one-to-one relation
if($fk instanceof Doctrine_ForeignKey || if($rel instanceof Doctrine_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $rel instanceof Doctrine_LocalKey) {
switch($fk->getType()): switch($rel->getType()):
case Doctrine_Relation::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
case Doctrine_Relation::MANY_AGGREGATE: case Doctrine_Relation::MANY_AGGREGATE:
// one-to-many relation found // one-to-many relation found
if( ! ($value instanceof Doctrine_Collection)) if( ! ($value instanceof Doctrine_Collection))
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references."); throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.");
$value->setReference($this,$fk); $value->setReference($this,$rel);
break; break;
case Doctrine_Relation::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Relation::ONE_AGGREGATE: case Doctrine_Relation::ONE_AGGREGATE:
@ -781,15 +805,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if( ! ($value instanceof Doctrine_Record)) if( ! ($value instanceof Doctrine_Record))
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Record when setting one-to-one references."); throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Record when setting one-to-one references.");
if($fk->getLocal() == $this->table->getIdentifier()) { if($rel->getLocal() == $this->table->getIdentifier()) {
$this->references[$name]->set($fk->getForeign(),$this); $this->references[$name]->set($rel->getForeign(),$this);
} else { } else {
$this->set($fk->getLocal(),$value); $this->set($rel->getLocal(),$value);
} }
break; break;
endswitch; endswitch;
} elseif($fk instanceof Doctrine_Association) { } elseif($rel instanceof Doctrine_Association) {
// join table relation found // join table relation found
if( ! ($value instanceof Doctrine_Collection)) if( ! ($value instanceof Doctrine_Collection))
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references."); throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.");
@ -980,8 +1004,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
/** /**
* saveAssociations * saveAssociations
*
* save the associations of many-to-many relations * save the associations of many-to-many relations
* this method also deletes associations that do not exist anymore * this method also deletes associations that do not exist anymore
*
* @return void * @return void
*/ */
final public function saveAssociations() { final public function saveAssociations() {
@ -1057,6 +1083,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
/** /**
* getOriginals * getOriginals
* returns an original collection of related component
*
* @return Doctrine_Collection
*/ */
final public function getOriginals($name) { final public function getOriginals($name) {
if( ! isset($this->originals[$name])) if( ! isset($this->originals[$name]))
@ -1079,13 +1108,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $conn->delete($this); return $conn->delete($this);
} }
/** /**
* copy
* returns a copy of this object * returns a copy of this object
* @return DAO *
* @return Doctrine_Record
*/ */
public function copy() { public function copy() {
return $this->table->create($this->data); return $this->table->create($this->data);
} }
/** /**
* assignIdentifier
*
* @param integer $id * @param integer $id
* @return void * @return void
*/ */
@ -1150,6 +1183,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* obtainReference * obtainReference
* *
* @param string $name * @param string $name
* @throws Doctrine_Record_Exception if trying to get an unknown related component
*/ */
public function obtainReference($name) { public function obtainReference($name) {
if(isset($this->references[$name])) if(isset($this->references[$name]))
@ -1218,7 +1252,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* loadReference * loadReference
* loads a related component * loads a related component
* *
* @throws InvalidKeyException * @throws Doctrine_Table_Exception if trying to load an unknown related component
* @param string $name * @param string $name
* @return void * @return void
*/ */

View File

@ -37,10 +37,11 @@ error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests"); $test = new GroupTest("Doctrine Framework Unit Tests");
$test->addTestCase(new Doctrine_DB_TestCase());
/**
$test->addTestCase(new Doctrine_ConnectionTestCase()); $test->addTestCase(new Doctrine_ConnectionTestCase());
$test->addTestCase(new Doctrine_DB_TestCase());
$test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_AccessTestCase()); $test->addTestCase(new Doctrine_AccessTestCase());
@ -74,17 +75,15 @@ $test->addTestCase(new Doctrine_ValueHolder_TestCase());
$test->addTestCase(new Doctrine_RawSql_TestCase()); $test->addTestCase(new Doctrine_RawSql_TestCase());
$test->addTestCase(new Doctrine_Query_Limit_TestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase());
*/
//$test->addTestCase(new Doctrine_SchemaTestCase()); //$test->addTestCase(new Doctrine_SchemaTestCase());
//$test->addTestCase(new Doctrine_ImportTestCase()); //$test->addTestCase(new Doctrine_ImportTestCase());
/**
$test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_Query_ReferenceModel_TestCase()); $test->addTestCase(new Doctrine_Query_ReferenceModel_TestCase());
$test->addTestCase(new Doctrine_BooleanTestCase());
$test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_EnumTestCase()); $test->addTestCase(new Doctrine_EnumTestCase());
@ -94,7 +93,8 @@ $test->addTestCase(new Doctrine_RelationAccessTestCase());
$test->addTestCase(new Doctrine_EventListener_Chain_TestCase()); $test->addTestCase(new Doctrine_EventListener_Chain_TestCase());
$test->addTestCase(new Doctrine_DataDict_Sqlite_TestCase()); $test->addTestCase(new Doctrine_DataDict_Sqlite_TestCase());
*/
$test->addTestCase(new Doctrine_BooleanTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());