Commit 611c65e7 authored by zYne's avatar zYne

added null key handling for Doctrine_Collection

parent 74736918
...@@ -425,7 +425,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -425,7 +425,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
{ {
if ( ! isset($this->data[$key])) { if ( ! isset($this->data[$key])) {
$this->expand($key); $this->expand($key);
throw new InvalidKeyException();
throw new Doctrine_Collection_Exception('Unknown key ' . $key);
} }
$removed = $this->data[$key]; $removed = $this->data[$key];
...@@ -437,7 +438,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -437,7 +438,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* contains * contains
* whether or not this collection contains a specified element * whether or not this collection contains a specified element
* *
* @param mixed $key * @param mixed $key the key of the element
* @return boolean * @return boolean
*/ */
public function contains($key) public function contains($key)
...@@ -445,11 +446,37 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -445,11 +446,37 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
return isset($this->data[$key]); return isset($this->data[$key]);
} }
/** /**
* @param mixed $key * get
* @return object Doctrine_Record return a specified record * 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) 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])) { if ( ! isset($this->data[$key])) {
$this->expand($key); $this->expand($key);
......
...@@ -85,6 +85,16 @@ class Doctrine_Collection_TestCase extends Doctrine_UnitTestCase { ...@@ -85,6 +85,16 @@ class Doctrine_Collection_TestCase extends Doctrine_UnitTestCase {
$this->connection->clear(); $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() { public function testLoadRelatedForNormalAssociation() {
$resource = new Doctrine_Collection('Resource'); $resource = new Doctrine_Collection('Resource');
......
...@@ -203,6 +203,15 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -203,6 +203,15 @@ class Doctrine_UnitTestCase extends UnitTestCase {
public function getConnection() { public function getConnection() {
return $this->connection; 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() { public function clearCache() {
foreach($this->tables as $name) { foreach($this->tables as $name) {
$table = $this->connection->getTable($name); $table = $this->connection->getTable($name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment