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

made sure every caller of getIdentifier can handle an array. getIdentifier now always returns an array.

This commit is contained in:
romanb 2008-02-24 20:31:49 +00:00
parent edcc8be207
commit 85cb20f6dd
15 changed files with 92 additions and 82 deletions

View File

@ -24,8 +24,9 @@
* These informations are used for the proper object-relational mapping of the class.
*
* @package Doctrine
* @author Roman Borschel <roman@code-factory.org>
* @since 1.0
* @subpackage ClassMetadata
* @author Roman Borschel <roman@code-factory.org>
* @since 1.0
*/
class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializable
{
@ -299,8 +300,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
*/
public function isIdentifier($fieldName)
{
return ($fieldName === $this->getIdentifier() ||
in_array($fieldName, (array) $this->getIdentifier()));
return in_array($fieldName, (array)$this->getIdentifier());
}
/**
@ -572,10 +572,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
}
if ( ! empty($options['primary'])) {
if (isset($this->_identifier)) {
$this->_identifier = $this->_identifier;
}
if ( ! in_array($fieldName, (array) $this->_identifier)) {
if ( ! in_array($fieldName, $this->_identifier)) {
$this->_identifier[] = $fieldName;
}
}
@ -657,7 +654,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
return $this->_identifier;
}
public function setIdentifier($identifier)
public function setIdentifier(array $identifier)
{
$this->_identifier = $identifier;
}

View File

@ -206,7 +206,7 @@ class Doctrine_ClassMetadata_Factory
*/
protected function _initIdentifier(Doctrine_ClassMetadata $class)
{
switch (count($class->getIdentifier())) {
switch (count((array)$class->getIdentifier())) {
case 0:
if ($class->getInheritanceType() == Doctrine::INHERITANCETYPE_JOINED &&
count($class->getOption('parents')) > 0) {
@ -242,12 +242,12 @@ class Doctrine_ClassMetadata_Factory
'autoincrement' => true,
'primary' => true);
$class->setColumn('id', $definition['type'], $definition['length'], $definition, true);
$class->setIdentifier('id');
$class->setIdentifier(array('id'));
$class->setIdentifierType(Doctrine::IDENTIFIER_AUTOINC);
}
break;
case 1:
foreach ($class->getIdentifier() as $pk) {
foreach ((array)$class->getIdentifier() as $pk) {
$columnName = $class->getColumnName($pk);
$thisColumns = $class->getColumns();
$e = $thisColumns[$columnName];
@ -291,7 +291,7 @@ class Doctrine_ClassMetadata_Factory
}
}
$class->setIdentifier($pk);
$class->setIdentifier(array($pk));
break;
default:

View File

@ -426,15 +426,30 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
public function getPrimaryKeys()
{
$list = array();
$name = $this->_mapper->getTable()->getIdentifier();
$idFieldNames = (array)$this->_mapper->getClassMetadata()->getIdentifier();
foreach ($this->data as $record) {
if (is_array($record) && isset($record[$name])) {
$list[] = $record[$name];
if (is_array($record)) {
if (count($idFieldNames) > 1) {
$id = array();
foreach ($idFieldNames as $fieldName) {
if (isset($record[$fieldName])) {
$id[] = $record[$fieldName];
}
}
$list[] = $id;
} else {
$idField = $idFieldNames[0];
if (isset($record[$idField])) {
$list[] = $record[$idField];
}
}
} else {
// @todo does not take composite keys into account
$list[] = $record->getIncremented();
}
}
return $list;
}
@ -615,7 +630,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$this->data[$key]->setRelated($name, $sub);
}
} else if ($rel instanceof Doctrine_Relation_Association) {
$identifier = $this->_mapper->getTable()->getIdentifier();
// @TODO composite key support
$identifier = (array)$this->_mapper->getClassMetadata()->getIdentifier();
$asf = $rel->getAssociationFactory();
$name = $table->getComponentName();
@ -625,7 +641,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
}
$sub = new Doctrine_Collection($rel->getForeignComponentName());
foreach ($coll as $k => $related) {
if ($related->get($local) == $record[$identifier]) {
$idField = $identifier[0];
if ($related->get($local) == $record[$idField]) {
$sub->add($related->get($name));
}
}

View File

@ -145,11 +145,7 @@ class Doctrine_Data_Export extends Doctrine_Data
}
// skip single primary keys, we need to maintain composite primary keys
$keys = $record->getTable()->getIdentifier();
if ( ! is_array($keys)) {
$keys = array($keys);
}
$keys = (array)$record->getTable()->getIdentifier();
if (count($keys) <= 1 && in_array($key, $keys)) {
continue;

View File

@ -300,11 +300,7 @@ class Doctrine_Data_Import extends Doctrine_Data
with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
$columns = array_keys($record->toArray());
$pks = $record->getTable()->getIdentifier();
if ( ! is_array($pks)) {
$pks = array($pks);
}
$pks = (array)$record->getTable()->getIdentifier();
foreach ($columns as $column) {

View File

@ -344,11 +344,7 @@ class Doctrine_Mapper extends Doctrine_Configurable implements Countable
public function getRecord(array $data)
{
if ( ! empty($data)) {
$identifierFieldNames = $this->_classMetadata->getIdentifier();
if ( ! is_array($identifierFieldNames)) {
$identifierFieldNames = array($identifierFieldNames);
}
$identifierFieldNames = (array)$this->_classMetadata->getIdentifier();
$found = false;
foreach ($identifierFieldNames as $fieldName) {

View File

@ -77,14 +77,14 @@ class Doctrine_Mapper_DefaultStrategy extends Doctrine_Mapper_Strategy
$seq = $class->getTableOption('sequenceName');
if ( ! empty($seq)) {
$id = $conn->sequence->nextId($seq);
$seqName = $class->getIdentifier();
$seqName = $identifier[0];
$fields[$seqName] = $id;
$record->assignIdentifier($id);
}
$this->_insertRow($class->getTableName(), $fields);
if (empty($seq) && count($identifier) == 1 && $identifier[0] == $class->getIdentifier() &&
if (empty($seq) && count($identifier) == 1 &&
$class->getIdentifierType() != Doctrine::IDENTIFIER_NATURAL) {
if (strtolower($conn->getName()) == 'pgsql') {
$seq = $class->getTableName() . '_' . $identifier[0];

View File

@ -33,8 +33,9 @@ class Doctrine_Mapper_JoinedStrategy extends Doctrine_Mapper_Strategy
} else if ($identifierType == Doctrine::IDENTIFIER_SEQUENCE) {
$seq = $record->getClassMetadata()->getTableOption('sequenceName');
if ( ! empty($seq)) {
$identifier = $conn->sequence->nextId($seq);
$dataSet[$parent][$parentClass->getIdentifier()] = $identifier;
$id = $conn->sequence->nextId($seq);
$identifierFields = (array)$parentClass->getIdentifier();
$dataSet[$parent][$identifierFields[0]] = $id;
$this->_insertRow($parentClass->getTableName(), $dataSet[$parent]);
}
} else {

View File

@ -1133,7 +1133,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
if ($needsSubQuery) {
$subquery = $this->getLimitSubquery();
// what about composite keys?
$idColumnName = $table->getColumnName($table->getIdentifier());
$idFieldNames = (array)$table->getIdentifier();
$idColumnName = $table->getColumnName($idFieldNames[0]);
switch (strtolower($this->_conn->getDriverName())) {
case 'mysql':
// mysql doesn't support LIMIT in subqueries
@ -1198,7 +1199,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
// get short alias
$alias = $this->getTableAlias($componentAlias);
// what about composite keys?
$primaryKey = $alias . '.' . $table->getColumnName($table->getIdentifier());
$idFieldNames = (array)$table->getIdentifier();
$primaryKey = $alias . '.' . $table->getColumnName($idFieldNames[0]);
// initialize the base of the subquery
$subquery = 'SELECT DISTINCT ' . $this->_conn->quoteIdentifier($primaryKey);
@ -1507,18 +1509,20 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
$assocAlias = $this->getTableAlias($assocPath, $asf->getTableName());
$queryPart = $join . $assocTableName . ' ' . $assocAlias;
$localTableIdFieldNames = (array)$localTable->getIdentifier();
$queryPart .= ' ON ' . $localAlias
. '.'
. $localTable->getColumnName($localTable->getIdentifier()) // what about composite keys?
. $localTable->getColumnName($localTableIdFieldNames[0]) // what about composite keys?
. ' = '
. $assocAlias . '.' . $relation->getLocal();
$tableIdFieldNames = (array)$table->getIdentifier();
if ($relation->isEqual()) {
// equal nest relation needs additional condition
$queryPart .= ' OR ' . $localAlias
. '.'
. $table->getColumnName($table->getIdentifier())
. $table->getColumnName($tableIdFieldNames[0])
. ' = '
. $assocAlias . '.' . $relation->getForeign();
}
@ -1535,19 +1539,20 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
}
$relationTable = $relation->getTable();
$queryPart .= $this->_conn->quoteIdentifier($foreignAlias . '.' . $relationTable->getColumnName($relationTable->getIdentifier()))
$relationTableIdFieldNames = (array)$relationTable->getIdentifier();
$queryPart .= $this->_conn->quoteIdentifier($foreignAlias . '.' . $relationTable->getColumnName($relationTableIdFieldNames[0]))
. ' = '
. $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getForeign());
if ($relation->isEqual()) {
$queryPart .= ' OR '
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $table->getColumnName($table->getIdentifier()))
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $table->getColumnName($tableIdFieldNames[0]))
. ' = '
. $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getLocal())
. ') AND '
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $table->getColumnName($table->getIdentifier()))
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $table->getColumnName($tableIdFieldNames[0]))
. ' != '
. $this->_conn->quoteIdentifier($localAlias . '.' . $table->getColumnName($table->getIdentifier()));
. $this->_conn->quoteIdentifier($localAlias . '.' . $table->getColumnName($tableIdFieldNames[0]));
}
}
} else {

View File

@ -536,10 +536,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
case Doctrine::IDENTIFIER_AUTOINC:
case Doctrine::IDENTIFIER_SEQUENCE:
case Doctrine::IDENTIFIER_NATURAL:
$name = $this->_table->getIdentifier();
if (is_array($name)) {
$name = $name[0];
}
$name = (array)$this->_table->getIdentifier();
$name = $name[0];
if ($exists) {
if (isset($this->_data[$name]) && $this->_data[$name] !== self::$_null) {
$this->_id[$name] = $this->_data[$name];
@ -547,7 +545,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
}
break;
case Doctrine::IDENTIFIER_COMPOSITE:
$names = $this->_table->getIdentifier();
$names = (array)$this->_table->getIdentifier();
foreach ($names as $name) {
if ($this->_data[$name] === self::$_null) {
@ -581,7 +579,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
unset($vars['_filter']);
unset($vars['_node']);
$name = $this->_table->getIdentifier();
//$name = (array)$this->_table->getIdentifier();
$this->_data = array_merge($this->_data, $this->_id);
foreach ($this->_data as $k => $v) {
@ -1005,7 +1003,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
if ( ! $rel->isOneToOne()) {
// one-to-many relation found
if ( ! ($value instanceof Doctrine_Collection)) {
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.");
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second"
. " argument should be an instance of Doctrine_Collection when"
. " setting one-to-many references.");
}
if (isset($this->_references[$name])) {
$this->_references[$name]->setData($value->getData());
@ -1019,10 +1019,13 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
// one-to-one relation found
if ( ! ($value instanceof Doctrine_Record)) {
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Record or Doctrine_Null when setting one-to-one references.");
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(),"
. " second argument should be an instance of Doctrine_Record"
. " or Doctrine_Null when setting one-to-one references.");
}
if ($rel instanceof Doctrine_Relation_LocalKey) {
if ( ! empty($foreignFieldName) && $foreignFieldName != $value->getTable()->getIdentifier()) {
$idFieldNames = (array)$value->getTable()->getIdentifier();
if ( ! empty($foreignFieldName) && $foreignFieldName != $idFieldNames[0]) {
$this->set($localFieldName, $value->rawGet($foreignFieldName), false);
} else {
$this->set($localFieldName, $value, false);
@ -1271,8 +1274,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
}
if ($this->_table->getIdentifierType() == Doctrine::IDENTIFIER_AUTOINC) {
$i = $this->_table->getIdentifier();
$a[$i] = $this->getIncremented();
$idFieldNames = (array)$this->_table->getIdentifier();
$id = $idFieldNames[0];
$a[$id] = $this->getIncremented();
}
if ($deep) {
@ -1465,8 +1469,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
$data = $this->_data;
if ($this->_table->getIdentifierType() === Doctrine::IDENTIFIER_AUTOINC) {
$id = $this->_table->getIdentifier();
$idFieldNames = (array)$this->_table->getIdentifier();
$id = $idFieldNames[0];
unset($data[$id]);
}
@ -1519,7 +1523,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
$this->_data[$fieldName] = $value;
}
} else {
$name = $this->_table->getIdentifier();
$idFieldNames = (array)$this->_table->getIdentifier();
$name = $idFieldNames[0];
$this->_id[$name] = $id;
$this->_data[$name] = $id;
}
@ -1734,7 +1739,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
->addWhere($rel->getForeign() . ' = ?', array_values($this->identifier()));
if (count($ids) > 0) {
$q->whereIn($rel->getTable()->getIdentifier(), $ids);
$relTableIdFieldNames = (array)$rel->getTable()->getIdentifier();
$q->whereIn($relTableIdFieldNames[0], $ids);
}
$q->execute();
@ -1802,7 +1808,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
->set($rel->getForeign(), '?', array_values($this->identifier()));
if (count($ids) > 0) {
$q->whereIn($rel->getTable()->getIdentifier(), $ids);
$relTableIdFieldNames = (array)$rel->getTable()->getIdentifier();
$q->whereIn($relTableIdFieldNames[0], $ids);
}
$q->execute();
@ -1815,7 +1822,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
->set($rel->getLocalFieldName(), '?', $ids);
if (count($ids) > 0) {
$q->whereIn($rel->getTable()->getIdentifier(), array_values($this->identifier()));
$relTableIdFieldNames = (array)$rel->getTable()->getIdentifier();
$q->whereIn($relTableIdFieldNames[0], array_values($this->identifier()));
}
$q->execute();

View File

@ -336,14 +336,14 @@ class Doctrine_Relation_Parser
public function getIdentifiers($table)
{
$componentNameToLower = strtolower($table->getComponentName());
if (is_array($table->getIdentifier())) {
$columns = array();
$idFieldNames = (array)$table->getIdentifier();
if (count($idFieldNames) > 1) {
$columns = array();
foreach ((array) $table->getIdentifierColumnNames() as $identColName) {
$columns[] = $componentNameToLower . '_' . $identColName;
}
} else {
$columns = $componentNameToLower . '_' . $table->getColumnName(
$table->getIdentifier());
$columns = $componentNameToLower . '_' . $table->getColumnName($idFieldNames[0]);
}
return $columns;
@ -405,15 +405,6 @@ class Doctrine_Relation_Parser
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getClassName()));
$localIdentifierColumnNames = $this->_table->getIdentifierColumnNames();
if ((count($localIdentifierColumnNames) - 1) < 0) {
echo $this->_table->getClassName();
var_dump($this->_table->getIdentifier());
try {
throw new Exception();
} catch (Exception $e) {
echo $e->getTraceAsString() . "<br />";
}
}
$localIdColumnName = $localIdentifierColumnNames[count($localIdentifierColumnNames) - 1];
$foreignIdentifierColumnNames = $def['table']->getIdentifierColumnNames();
$foreignIdColumnName = $foreignIdentifierColumnNames[count($foreignIdentifierColumnNames) - 1];

View File

@ -41,7 +41,7 @@ class Doctrine_Validator_Unique
public function validate($value)
{
$table = $this->invoker->getTable();
$pks = $table->getIdentifier();
$pks = (array)$table->getIdentifier();
if ( is_array($pks) ) {
$pks = join(',', $pks);

View File

@ -40,7 +40,8 @@ class Doctrine_Collection_Offset_TestCase extends Doctrine_UnitTestCase {
$this->connection->setAttribute(Doctrine::ATTR_COLL_LIMIT, 1);
$users = $this->connection->query("FROM User-b, User.Phonenumber-o WHERE User.".$this->objTable->getIdentifier()." = 5");
$idFieldNames = (array)$this->objTable->getIdentifier();
$users = $this->connection->query("FROM User-b, User.Phonenumber-o WHERE User.".$idFieldNames[0]." = 5");
$this->assertEqual(count($users), 1);

View File

@ -269,7 +269,7 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase
public function testCompositePK() {
$record = new EntityReference();
$this->assertEqual($record->getTable()->getIdentifier(), array("entity1","entity2"));
$this->assertEqual((array)$record->getTable()->getIdentifier(), array("entity1","entity2"));
$this->assertEqual($record->getTable()->getIdentifierType(), Doctrine::IDENTIFIER_COMPOSITE);
$this->assertEqual($record->identifier(), array("entity1" => null, "entity2" => null));
$this->assertEqual($record->state(), Doctrine_Record::STATE_TCLEAN);

View File

@ -110,14 +110,16 @@ class Doctrine_Table_TestCase extends Doctrine_UnitTestCase
$this->assertTrue($fk->getType() == Doctrine_Relation::ONE_AGGREGATE);
$this->assertTrue($fk->getLocal() == "email_id");
$this->assertTrue($fk->getForeign() == $fk->getTable()->getIdentifier());
$fkIdFieldNames = (array)$fk->getTable()->getIdentifier();
$this->assertTrue($fk->getForeign() == $fkIdFieldNames[0]);
$fk = $this->objTable->getTable()->getRelation('Phonenumber');
$this->assertTrue($fk instanceof Doctrine_Relation_ForeignKey);
$this->assertTrue($fk->getTable() instanceof Doctrine_ClassMetadata);
$this->assertTrue($fk->getType() == Doctrine_Relation::MANY);
$this->assertTrue($fk->getLocal() == $this->objTable->getTable()->getIdentifier());
$objTableIdFieldNames = (array)$this->objTable->getTable()->getIdentifier();
$this->assertTrue($fk->getLocal() == $objTableIdFieldNames[0]);
$this->assertTrue($fk->getForeign() == 'entity_id');