Changes to make hasOne relations with 'foreign' and 'local' work as expected
This commit is contained in:
parent
5aaa598fc5
commit
7f1666efc0
@ -883,7 +883,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
|
|||||||
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 ($rel instanceof Doctrine_Relation_LocalKey) {
|
||||||
$this->set($rel->getLocal(), $value, false);
|
$this->set($rel->getLocal(), $value->rawGet($rel->getForeign()), false);
|
||||||
} else {
|
} else {
|
||||||
$value->set($rel->getForeign(), $this, false);
|
$value->set($rel->getForeign(), $this, false);
|
||||||
}
|
}
|
||||||
|
@ -43,12 +43,18 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation
|
|||||||
*/
|
*/
|
||||||
public function fetchRelatedFor(Doctrine_Record $record)
|
public function fetchRelatedFor(Doctrine_Record $record)
|
||||||
{
|
{
|
||||||
$id = $record->get($this->definition['local']);
|
$id = $record->get($this->definition['local']);
|
||||||
|
|
||||||
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
|
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
|
||||||
$related = $this->getTable()->create();
|
$related = $this->getTable()->create();
|
||||||
} else {
|
} else {
|
||||||
$related = $this->getTable()->find($id);
|
$dql = 'FROM ' . $this->getTable()->getComponentName()
|
||||||
|
. ' WHERE ' . $this->getCondition();
|
||||||
|
|
||||||
|
$related = $this->getTable()
|
||||||
|
->getConnection()
|
||||||
|
->query($dql, array($id))
|
||||||
|
->getFirst();
|
||||||
|
|
||||||
if ( ! $related) {
|
if ( ! $related) {
|
||||||
$related = $this->getTable()->create();
|
$related = $this->getTable()->create();
|
||||||
@ -58,5 +64,19 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation
|
|||||||
$record->set($this->definition['local'], $related, false);
|
$record->set($this->definition['local'], $related, false);
|
||||||
|
|
||||||
return $related;
|
return $related;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getCondition
|
||||||
|
*
|
||||||
|
* @param string $alias
|
||||||
|
*/
|
||||||
|
public function getCondition($alias = null)
|
||||||
|
{
|
||||||
|
if ( ! $alias) {
|
||||||
|
$alias = $this->getTable()->getComponentName();
|
||||||
|
}
|
||||||
|
return $alias . '.' . $this->definition['foreign'] . ' = ?';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user