From 1a0179bdce0614ec0ba20fca93a5ca3baf8ae525 Mon Sep 17 00:00:00 2001 From: zYne Date: Wed, 27 Sep 2006 21:34:32 +0000 Subject: [PATCH] Fixes #128 Ticket: 128 --- lib/Doctrine/Association/Self.php | 34 +++++++++++++++++++++++++++++++ lib/Doctrine/Record.php | 33 +++--------------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/lib/Doctrine/Association/Self.php b/lib/Doctrine/Association/Self.php index ad1b7bbb9..a6af700f1 100644 --- a/lib/Doctrine/Association/Self.php +++ b/lib/Doctrine/Association/Self.php @@ -52,5 +52,39 @@ class Doctrine_Association_Self extends Doctrine_Association { 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)); + } } ?> diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 55158a7d5..95332e612 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -594,6 +594,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @param $name name of the property * @return mixed */ + public function rawGet($name) { if( ! isset($this->data[$name])) throw new InvalidKeyException(); @@ -603,6 +604,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite return $this->data[$name]; } + /** * load * loads all the unitialized properties from the database @@ -1299,36 +1301,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $coll = $graph->query($query,array($id)); $coll->setReference($this, $fk); } elseif($fk instanceof Doctrine_Association_Self) { - $id = $this->getIncremented(); - - $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)); + $coll = $fk->fetchRelatedFor($this); } elseif($fk instanceof Doctrine_Association) { $id = $this->getIncremented(); $coll = $graph->query($query, array($id));