From 611c65e759f86cf774c914aaf66eb471d3f2ed4a Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 30 Dec 2006 00:50:54 +0000 Subject: [PATCH] added null key handling for Doctrine_Collection --- lib/Doctrine/Collection.php | 35 +++++++++++++++++++++++++++++++---- tests/CollectionTestCase.php | 10 ++++++++++ tests/UnitTestCase.php | 9 +++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index 6addc946a..86af3cee5 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -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); diff --git a/tests/CollectionTestCase.php b/tests/CollectionTestCase.php index 26a0b3896..4d9f93bc7 100644 --- a/tests/CollectionTestCase.php +++ b/tests/CollectionTestCase.php @@ -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'); diff --git a/tests/UnitTestCase.php b/tests/UnitTestCase.php index 0e4194e54..224b70a9a 100644 --- a/tests/UnitTestCase.php +++ b/tests/UnitTestCase.php @@ -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);