From da9d179d9fa367d172b3592fb28c261048ec1e82 Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 1 Sep 2006 21:30:18 +0000 Subject: [PATCH] Support for mapping table column values as collection indexes --- Doctrine/Collection.php | 53 ++++++++++++++++++++------------- Doctrine/Configurable.php | 3 -- tests/CollectionTestCase.php | 57 +++++++++++++++++++++++++++++++----- tests/run.php | 5 ++-- 4 files changed, 85 insertions(+), 33 deletions(-) diff --git a/Doctrine/Collection.php b/Doctrine/Collection.php index e6e0536a3..ecc211f8e 100644 --- a/Doctrine/Collection.php +++ b/Doctrine/Collection.php @@ -57,9 +57,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator */ protected $expanded = array(); /** - * @var mixed $generator + * @var string $keyColumn */ - protected $generator; + protected $keyColumn; /** * @var Doctrine_Null $null used for extremely fast SQL null value testing */ @@ -80,7 +80,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $name = $table->getAttribute(Doctrine::ATTR_COLL_KEY); if($name !== null) { - $this->generator = new Doctrine_IndexGenerator($name); + $this->keyColumn = $name; } } /** @@ -137,7 +137,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $name = $this->table->getAttribute(Doctrine::ATTR_COLL_KEY); if($name !== null) { - $this->generator = new Doctrine_IndexGenerator($name); + $this->keyColumn = $name; } } /** @@ -155,17 +155,21 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator return $this->expandable; } /** - * @param Doctrine_IndexGenerator $generator + * setKeyColumn + * + * @param string $column * @return void */ - public function setGenerator($generator) { - $this->generator = $generator; + public function setKeyColumn($column) { + $this->keyColumn = $column; } /** - * @return Doctrine_IndexGenerator + * getKeyColumn + * + * @return string */ - public function getGenerator() { - return $this->generator; + public function getKeyColumn() { + return $this->column; } /** * @return array @@ -450,9 +454,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator return true; } - if(isset($this->generator)) { - $key = $this->generator->getIndex($record); - $this->data[$key] = $record; + if(isset($this->keyColumn)) { + $value = $record->get($this->keyColumn); + if($value === null) + throw new Doctrine_Exception("Couldn't create collection index. Record field '".$this->keyColumn."' was null."); + + $this->data[$value] = $record; } else $this->data[] = $record; @@ -480,9 +487,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator return true; } - if(isset($this->generator)) { - $key = $this->generator->getIndex($record); - $this->data[$key] = $record; + if(isset($this->keyColumn)) { + $value = $record->get($this->keyColumn); + if($value === null) + throw new Doctrine_Exception("Couldn't create collection index. Record field '".$this->keyColumn."' was null."); + + $this->data[$value] = $record; } else $this->data[] = $record; @@ -510,11 +520,14 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator } elseif($this instanceof Doctrine_Collection_Batch) { $this->data = $query->getData($name); - if(isset($this->generator)) { + if(isset($this->keyColumn)) { foreach($this->data as $k => $v) { - $record = $this->get($k); - $i = $this->generator->getIndex($record); - $this->data[$i] = $record; + + $value = $record->get($this->keyColumn); + if($value === null) + throw new Doctrine_Exception("Couldn't create collection index. Record field '".$this->keyColumn."' was null."); + + $this->data[$value] = $record; unset($this->data[$k]); } } diff --git a/Doctrine/Configurable.php b/Doctrine/Configurable.php index 2d403e758..8cd625b0a 100644 --- a/Doctrine/Configurable.php +++ b/Doctrine/Configurable.php @@ -56,9 +56,6 @@ abstract class Doctrine_Configurable { $dir = dirname(__FILE__); $value = $dir.substr($value,6); } - - if(! is_dir($value) && ! file_exists($value)) - mkdir($value,0777); break; case Doctrine::ATTR_CACHE_TTL: if($value < 1) diff --git a/tests/CollectionTestCase.php b/tests/CollectionTestCase.php index 172036e73..15cf1611c 100644 --- a/tests/CollectionTestCase.php +++ b/tests/CollectionTestCase.php @@ -170,18 +170,14 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { $this->assertEqual($coll[2]->getState(), Doctrine_Record::STATE_PROXY); - $generator = new Doctrine_IndexGenerator($this->objTable->getIdentifier()); - $coll->setGenerator($generator); - $generator = $coll->getGenerator(); - + + $coll->setKeyColumn('id'); $user = $this->connection->getTable("User")->find(4); - $this->assertEqual($generator->getIndex($user), 4); } public function testGenerator() { - $generator = new Doctrine_IndexGenerator("name"); $coll = new Doctrine_Collection($this->objTable); - $coll->setGenerator($generator); + $coll->setKeyColumn('name'); $user = new User(); $user->name = "name"; @@ -197,5 +193,52 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { } } + public function testFetchCollectionWithIdAsIndex() { + $user = new User(); + $user->setAttribute(Doctrine::ATTR_COLL_KEY, 'id'); + + $users = $user->getTable()->findAll(); + $this->assertFalse($users->contains(0)); + $this->assertEqual($users->count(), 8); + + $this->assertEqual($users[0]->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertEqual($users[4]->getState(), Doctrine_Record::STATE_CLEAN); + } + public function testFetchCollectionWithNameAsIndex() { + $user = new User(); + $user->setAttribute(Doctrine::ATTR_COLL_KEY, 'name'); + + $users = $user->getTable()->findAll(); + $this->assertFalse($users->contains(0)); + $this->assertEqual($users->count(), 8); + + $this->assertEqual($users[0]->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertEqual($users['zYne']->getState(), Doctrine_Record::STATE_CLEAN); + } + public function testFetchMultipleCollections() { + $this->connection->clear(); + + $user = new User(); + $user->setAttribute(Doctrine::ATTR_COLL_KEY, 'id'); + $phonenumber = new Phonenumber(); + $phonenumber->setAttribute(Doctrine::ATTR_COLL_KEY, 'id'); + + + $q = new Doctrine_Query(); + $users = $q->from('User.Phonenumber')->execute(); + $this->assertFalse($users->contains(0)); + $this->assertEqual($users->count(), 8); + + $this->assertEqual($users[0]->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertEqual($users[2]->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertEqual($users[3]->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertEqual($users[4]->getState(), Doctrine_Record::STATE_CLEAN); + $this->assertEqual($users[4]->name, 'zYne'); + + $this->assertEqual($users[4]->Phonenumber[0]->exists(), false); + $this->assertEqual($users[4]->Phonenumber[0]->getState(), Doctrine_Record::STATE_TDIRTY); + $this->assertEqual($users[4]->Phonenumber[1]->exists(), false); + $this->assertEqual($users[4]->Phonenumber[2]->getState(), Doctrine_Record::STATE_CLEAN); + } } ?> diff --git a/tests/run.php b/tests/run.php index c720045f1..8397f9313 100644 --- a/tests/run.php +++ b/tests/run.php @@ -62,9 +62,7 @@ $test->addTestCase(new Doctrine_Filter_TestCase()); $test->addTestCase(new Doctrine_ValueHolder_TestCase()); -$test->addTestCase(new Doctrine_ValidatorTestCase()); - -$test->addTestCase(new Doctrine_CollectionTestCase()); +$test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_QueryTestCase()); @@ -76,6 +74,7 @@ $test->addTestCase(new Doctrine_SchemaTestCase()); $test->addTestCase(new Doctrine_ImportTestCase()); +$test->addTestCase(new Doctrine_CollectionTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());