Support for mapping table column values as collection indexes
This commit is contained in:
parent
155f5193ec
commit
da9d179d9f
@ -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]);
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -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());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user