1
0
mirror of synced 2024-12-13 06:46:03 +03:00

added null key handling for Doctrine_Collection

This commit is contained in:
zYne 2006-12-30 00:50:54 +00:00
parent 747369185b
commit 611c65e759
3 changed files with 50 additions and 4 deletions

View File

@ -425,7 +425,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
{
if ( ! isset($this->data[$key])) {
$this->expand($key);
throw new InvalidKeyException();
throw new Doctrine_Collection_Exception('Unknown key ' . $key);
}
$removed = $this->data[$key];
@ -437,7 +438,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* contains
* whether or not this collection contains a specified element
*
* @param mixed $key
* @param mixed $key the key of the element
* @return boolean
*/
public function contains($key)
@ -445,11 +446,37 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
return isset($this->data[$key]);
}
/**
* @param mixed $key
* @return object Doctrine_Record return a specified record
* get
* returns a record for given key
*
* There are two special cases:
*
* 1. if null is given as a key a new record is created and attached
* at the end of the collection
*
* 2. if given key does not exist, then a new record is create and attached
* to the given key
*
* Collection also maps referential information to newly created records
*
* @param mixed $key the key of the element
* @return Doctrine_Record return a specified record
*/
public function get($key)
{
if($key === null) {
$record = $this->table->create();
if (isset($this->reference_field)) {
$record->set($this->reference_field, $this->reference, false);
}
$this->data[] = $record;
return $record;
}
if ( ! isset($this->data[$key])) {
$this->expand($key);

View File

@ -85,6 +85,16 @@ class Doctrine_Collection_TestCase extends Doctrine_UnitTestCase {
$this->connection->clear();
}
public function testOffsetGetWithNullArgumentReturnsNewRecord()
{
$coll = new Doctrine_Collection('User');
$this->assertEqual($coll->count(), 0);
$coll[]->name = 'zYne';
$this->assertEqual($coll->count(), 1);
$this->assertEqual($coll[0]->name, 'zYne');
}
public function testLoadRelatedForNormalAssociation() {
$resource = new Doctrine_Collection('Resource');

View File

@ -203,6 +203,15 @@ class Doctrine_UnitTestCase extends UnitTestCase {
public function getConnection() {
return $this->connection;
}
public function assertDeclarationType($type, $type2) {
$dec = $this->getDeclaration($type);
if( ! is_array($type2))
$type2 = array($type2);
$this->assertEqual($dec[0], $type2);
}
public function getDeclaration($type) {
return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
}
public function clearCache() {
foreach($this->tables as $name) {
$table = $this->connection->getTable($name);