Commit 3dca74f2 authored by zYne's avatar zYne

Association self-referencing fetching now works (still not integrated into DQL)

parent fadef335
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Association this class takes care of association mapping
* (= many-to-many relationships, where the relationship is handled with an additional relational table
......@@ -13,7 +32,7 @@ class Doctrine_Association extends Doctrine_Relation {
/**
* @var Doctrine_Table $associationTable
*/
private $associationTable;
protected $associationTable;
/**
* the constructor
* @param Doctrine_Table $table foreign factory object
......@@ -48,7 +67,7 @@ class Doctrine_Association extends Doctrine_Relation {
" IN (".substr(str_repeat("?, ", $count),0,-2).")";
$dql = "FROM ".$this->table->getComponentName();
$dql .= ":".$this->associationTable->getComponentName();
$dql .= ".".$this->associationTable->getComponentName();
$dql .= " WHERE ".$this->table->getComponentName().".".$this->table->getIdentifier()." IN ($sub)";
break;
case "collection":
......
......@@ -712,8 +712,8 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if( ! $loadFields) {
$this->subqueryAliases[] = $assocTableName;
}
$this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal();
$this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign();
$this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".".$table->getIdentifier()." = ".$assocTableName.".".$fk->getLocal();
$this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".".$table->getIdentifier()." = ".$assocTableName.".".$fk->getForeign();
}
$this->joins[$tname2] = $prevTable;
......
......@@ -123,8 +123,6 @@ class Doctrine_RawSql extends Doctrine_Hydrate {
* @return string
*/
public function getQuery() {
foreach($this->fields as $field) {
$e = explode(".", $field);
if( ! isset($e[1]))
......
......@@ -1201,6 +1201,37 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$id = $this->get($local);
$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));
} elseif($fk instanceof Doctrine_Association) {
$id = $this->getIncremented();
$coll = $graph->query($query, array($id));
......
......@@ -622,7 +622,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
// SELF-REFERENCING THROUGH JOIN TABLE
$this->relations[$e2[0]] = new Doctrine_ForeignKey($associationTable,$local,$fields[0],Doctrine_Relation::MANY_COMPOSITE, $e2[0]);
$relation = new Doctrine_Association($table,$associationTable,$fields[0],$fields[1], $type, $alias);
$relation = new Doctrine_Association_Self($table,$associationTable,$fields[0],$fields[1], $type, $alias);
} else {
// auto initialize a new one-to-one relationship for association table
......
......@@ -64,6 +64,14 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($array[0]['MAX(entity.id)'], 11);
$this->assertEqual($array[0]['MIN(email.address)'], 'arnold@example.com');
$this->assertEqual($array[0]['COUNT(1)'], 14);
/**
$q = new Doctrine_Query();
$q->from("User.Phonenumber(COUNT(id))")->groupby("User.id");
$coll = $q->execute();
print Doctrine_Lib::formatSql($q->getQuery());
print_r($coll);
$this->assertEqual(count($coll), 8);
*/
}
......@@ -1224,5 +1232,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
//$this->assertTrue(isset($values['max']));
}
}
?>
......@@ -7,6 +7,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->tables[] = "enumTest";
parent::prepareTables();
}
public function testJoinTableSelfReferencingInsertingData() {
$e = new Entity();
$e->name = "Entity test";
......@@ -66,25 +67,14 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertEqual(count($result), 6);
$q = "SELECT entity.id AS entity__id,
entity.name AS entity__name,
entity.loginname AS entity__loginname,
entity.password AS entity__password,
entity.type AS entity__type,
entity.created AS entity__created,
entity.updated AS entity__updated,
entity.email_id AS entity__email_id,
entity_reference.entity1 AS entity_reference__entity1,
entity_reference.entity2 AS entity_reference__entity2
FROM entity
INNER JOIN entity_reference
ON entity.id = entity_reference.entity1
WHERE entity.id IN (SELECT entity2 FROM entity_reference WHERE entity1 IN (?))";
/**
$stmt = $this->dbh->prepare($q);
$stmt->execute(array(18));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
*/
$this->connection->clear();
$e = $e->getTable()->find($e->id);
......@@ -96,6 +86,8 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($e->Entity[0] instanceof Entity);
$this->assertTrue($e->Entity[1] instanceof Entity);
$this->assertEqual(count($this->dbh), ($count + 1));
$this->assertEqual($e->Entity[0]->name, "Friend 1");
......@@ -128,7 +120,9 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($coll->count(), 1);
}
/**
public function testToArray() {
$user = new User();
......@@ -923,6 +917,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$user = $this->connection->getTable("User")->find(4);
$this->assertTrue($user->getIterator() instanceof ArrayIterator);
}
*/
}
?>
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