This commit is contained in:
parent
17349244b0
commit
5227d51e7e
@ -43,15 +43,16 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
|
|||||||
*/
|
*/
|
||||||
public function fetchRelatedFor(Doctrine_Record $record)
|
public function fetchRelatedFor(Doctrine_Record $record)
|
||||||
{
|
{
|
||||||
$id = $record->get($this->definition['local']);
|
$id = array();
|
||||||
|
foreach ((array) $this->definition['local'] as $local) {
|
||||||
|
$id = $record->get($local);
|
||||||
|
}
|
||||||
if ($this->isOneToOne()) {
|
if ($this->isOneToOne()) {
|
||||||
if (empty($id)) {
|
if (empty($id)) {
|
||||||
$related = $this->getTable()->create();
|
$related = $this->getTable()->create();
|
||||||
} else {
|
} else {
|
||||||
$dql = 'FROM ' . $this->getTable()->getComponentName()
|
$dql = 'FROM ' . $this->getTable()->getComponentName()
|
||||||
. ' WHERE ' . $this->getTable()->getComponentName()
|
. ' WHERE ' . $this->getCondition();
|
||||||
. '.' . $this->definition['foreign'] . ' = ?';
|
|
||||||
|
|
||||||
$coll = $this->getTable()->getConnection()->query($dql, array($id));
|
$coll = $this->getTable()->getConnection()->query($dql, array($id));
|
||||||
$related = $coll[0];
|
$related = $coll[0];
|
||||||
@ -71,4 +72,20 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
|
|||||||
}
|
}
|
||||||
return $related;
|
return $related;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* getCondition
|
||||||
|
*
|
||||||
|
* @param string $alias
|
||||||
|
*/
|
||||||
|
public function getCondition($alias = null)
|
||||||
|
{
|
||||||
|
if ( ! $alias) {
|
||||||
|
$alias = $this->getTable()->getComponentName();
|
||||||
|
}
|
||||||
|
$conditions = array();
|
||||||
|
foreach ((array) $this->definition['foreign'] as $foreign) {
|
||||||
|
$conditions[] = $alias . '.' . $foreign . ' = ?';
|
||||||
|
}
|
||||||
|
return implode(' AND ', $conditions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,11 +106,6 @@ class Doctrine_Relation_Parser
|
|||||||
} else {
|
} else {
|
||||||
$alias = $name;
|
$alias = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! isset($options['definer'])) {
|
|
||||||
throw new Doctrine_Relation_Exception('Relation definer not set.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! isset($options['type'])) {
|
if ( ! isset($options['type'])) {
|
||||||
throw new Doctrine_Relation_Exception('Relation type not set.');
|
throw new Doctrine_Relation_Exception('Relation type not set.');
|
||||||
}
|
}
|
||||||
@ -189,6 +184,32 @@ class Doctrine_Relation_Parser
|
|||||||
|
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
public function guessColumns($classes, Doctrine_Table $foreignTable)
|
||||||
|
{
|
||||||
|
$conn = $this->_table->getConnection();
|
||||||
|
|
||||||
|
foreach ($classes as $class) {
|
||||||
|
$table = $conn->getTable($class);
|
||||||
|
$columns = $this->getIdentifiers($table);
|
||||||
|
$found = true;
|
||||||
|
|
||||||
|
foreach ((array) $columns as $column) {
|
||||||
|
if ( ! $foreignTable->hasColumn($column)) {
|
||||||
|
$found = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($found) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! $found) {
|
||||||
|
throw new Doctrine_Relation_Exception("Couldn't find columns.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Completes the given definition
|
* Completes the given definition
|
||||||
*
|
*
|
||||||
@ -199,17 +220,16 @@ class Doctrine_Relation_Parser
|
|||||||
{
|
{
|
||||||
$conn = $this->_table->getConnection();
|
$conn = $this->_table->getConnection();
|
||||||
$def['table'] = $conn->getTable($def['class']);
|
$def['table'] = $conn->getTable($def['class']);
|
||||||
$def['definer'] = $conn->getTable($def['definer']);
|
$foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
|
||||||
|
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
|
||||||
|
|
||||||
if (isset($def['local'])) {
|
if (isset($def['local'])) {
|
||||||
if ( ! isset($def['foreign'])) {
|
if ( ! isset($def['foreign'])) {
|
||||||
// local key is set, but foreign key is not
|
// local key is set, but foreign key is not
|
||||||
// try to guess the foreign key
|
// try to guess the foreign key
|
||||||
|
|
||||||
if ($def['local'] === $def['definer']->getIdentifier()) {
|
if ($def['local'] === $this->_table->getIdentifier()) {
|
||||||
$columns = $this->getIdentifiers($def['definer']);
|
$def['foreign'] = $this->guessColumns($localClasses, $def['table']);
|
||||||
|
|
||||||
$def['foreign'] = $columns;
|
|
||||||
} else {
|
} else {
|
||||||
// the foreign field is likely to be the
|
// the foreign field is likely to be the
|
||||||
// identifier of the foreign class
|
// identifier of the foreign class
|
||||||
@ -220,29 +240,27 @@ class Doctrine_Relation_Parser
|
|||||||
if (isset($def['foreign'])) {
|
if (isset($def['foreign'])) {
|
||||||
// local key not set, but foreign key is set
|
// local key not set, but foreign key is set
|
||||||
// try to guess the local key
|
// try to guess the local key
|
||||||
if ($def['foreign'] === $def['definer']->getIdentifier()) {
|
if ($def['foreign'] === $def['table']->getIdentifier()) {
|
||||||
$columns = $this->getIdentifiers($def['table']);
|
$def['local'] = $this->guessColumns($foreignClasses, $this->_table);
|
||||||
|
|
||||||
$def['local'] = $columns;
|
|
||||||
} else {
|
} else {
|
||||||
$def['local'] = $def['definer']->getIdentifier();
|
$def['local'] = $this->_table->getIdentifier();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// neither local or foreign key is being set
|
// neither local or foreign key is being set
|
||||||
// try to guess both keys
|
// try to guess both keys
|
||||||
|
|
||||||
$column = strtolower($def['definer']->getComponentName())
|
$column = strtolower($this->_table->getComponentName())
|
||||||
. '_' . $def['definer']->getIdentifier();
|
. '_' . $this->_table->getIdentifier();
|
||||||
|
|
||||||
if ($def['table']->hasColumn($column)) {
|
if ($def['table']->hasColumn($column)) {
|
||||||
$def['foreign'] = $column;
|
$def['foreign'] = $column;
|
||||||
$def['local'] = $def['definer']->getIdentifier();
|
$def['local'] = $this->_table->getIdentifier();
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$column = strtolower($def['table']->getComponentName())
|
$column = strtolower($def['table']->getComponentName())
|
||||||
. '_' . $def['table']->getIdentifier();
|
. '_' . $def['table']->getIdentifier();
|
||||||
|
|
||||||
if ($def['definer']->hasColumn($column)) {
|
if ($this->_table->hasColumn($column)) {
|
||||||
$def['foreign'] = $def['table']->getIdentifier();
|
$def['foreign'] = $def['table']->getIdentifier();
|
||||||
$def['local'] = $column;
|
$def['local'] = $column;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user