Commit 1a0179bd authored by zYne's avatar zYne

Fixes #128

Ticket: 128
parent 7b84d155
...@@ -52,5 +52,39 @@ class Doctrine_Association_Self extends Doctrine_Association { ...@@ -52,5 +52,39 @@ class Doctrine_Association_Self extends Doctrine_Association {
return $dql; return $dql;
} }
public function fetchRelatedFor(Doctrine_Record $record) {
$id = $record->getIncremented();
$q = new Doctrine_RawSql();
$assocTable = $this->getAssociationFactory()->getTableName();
$tableName = $record->getTable()->getTableName();
$identifier = $record->getTable()->getIdentifier();
$sub = "SELECT ".$this->getForeign().
" FROM ".$assocTable.
" WHERE ".$this->getLocal().
" = ?";
$sub2 = "SELECT ".$this->getLocal().
" FROM ".$assocTable.
" WHERE ".$this->getForeign().
" = ?";
$q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
->from($tableName.' INNER JOIN '.$assocTable.' ON '.
$tableName.'.'.$identifier.' = '.$assocTable.'.'.$this->getLocal().' OR '.
$tableName.'.'.$identifier.' = '.$assocTable.'.'.$this->getForeign()
)
->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '.
$tableName.'.'.$identifier.' IN ('.$sub2.')'
);
$q->addComponent($tableName, $record->getTable()->getComponentName());
$q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
return $q->execute(array($id, $id));
}
} }
?> ?>
...@@ -594,6 +594,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -594,6 +594,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @param $name name of the property * @param $name name of the property
* @return mixed * @return mixed
*/ */
public function rawGet($name) { public function rawGet($name) {
if( ! isset($this->data[$name])) if( ! isset($this->data[$name]))
throw new InvalidKeyException(); throw new InvalidKeyException();
...@@ -603,6 +604,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -603,6 +604,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $this->data[$name]; return $this->data[$name];
} }
/** /**
* load * load
* loads all the unitialized properties from the database * loads all the unitialized properties from the database
...@@ -1299,36 +1301,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1299,36 +1301,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$coll = $graph->query($query,array($id)); $coll = $graph->query($query,array($id));
$coll->setReference($this, $fk); $coll->setReference($this, $fk);
} elseif($fk instanceof Doctrine_Association_Self) { } elseif($fk instanceof Doctrine_Association_Self) {
$id = $this->getIncremented(); $coll = $fk->fetchRelatedFor($this);
$q = new Doctrine_RawSql();
$assocTable = $fk->getAssociationFactory()->getTableName();
$tableName = $this->getTable()->getTableName();
$identifier = $this->getTable()->getIdentifier();
$sub = "SELECT ".$fk->getForeign().
" FROM ".$assocTable.
" WHERE ".$fk->getLocal().
" = ?";
$sub2 = "SELECT ".$fk->getLocal().
" FROM ".$assocTable.
" WHERE ".$fk->getForeign().
" = ?";
$q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
->from($tableName.' INNER JOIN '.$assocTable.' ON '.
$tableName.'.'.$identifier.' = '.$assocTable.'.'.$fk->getLocal().' OR '.
$tableName.'.'.$identifier.' = '.$assocTable.'.'.$fk->getForeign()
)
->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '.
$tableName.'.'.$identifier.' IN ('.$sub2.')'
);
$q->addComponent($tableName, $this->table->getComponentName());
$q->addComponent($assocTable, $this->table->getComponentName().'.'.$fk->getAssociationFactory()->getComponentName());
$coll = $q->execute(array($id, $id));
} elseif($fk instanceof Doctrine_Association) { } elseif($fk instanceof Doctrine_Association) {
$id = $this->getIncremented(); $id = $this->getIncremented();
$coll = $graph->query($query, array($id)); $coll = $graph->query($query, array($id));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment