Commit 56a8f5cd authored by Guilherme Blanco's avatar Guilherme Blanco

[2.0][DDC-448][DDC-513] Fixed issue with Joined Inheritance Type and One To One Associations.

parent b6f9cd0c
......@@ -465,6 +465,11 @@ class SqlWalker implements TreeWalker
$fieldName = array_pop($parts);
$dqlAlias = $pathExpr->identificationVariable;
$class = $this->_queryComponents[$dqlAlias]['metadata'];
if (isset($class->inheritedAssociationFields[$fieldName])) {
$class = $this->_em->getClassMetadata($class->inheritedAssociationFields[$fieldName]);
}
$assoc = $class->associationMappings[$fieldName];
if ($assoc->isOwningSide) {
......@@ -472,7 +477,7 @@ class SqlWalker implements TreeWalker
if (count($assoc->sourceToTargetKeyColumns) > 1) {
throw QueryException::associationPathCompositeKeyNotSupported();
}
$sql .= $this->walkIdentificationVariable($dqlAlias) . '.'
$sql .= $this->getSqlTableAlias($class->table['name'], $dqlAlias) . '.'
. reset($assoc->targetToSourceKeyColumns);
} else {
// 2- Inverse side: NOT (YET?) SUPPORTED
......@@ -683,23 +688,15 @@ class SqlWalker implements TreeWalker
$joinAssocPathExpr = $join->joinAssociationPathExpression;
$joinedDqlAlias = $join->aliasIdentificationVariable;
$targetQComp = $this->_queryComponents[$joinedDqlAlias];
$targetClass = $targetQComp['metadata'];
$relation = $targetQComp['relation'];
$sourceClass = $this->_queryComponents[$joinAssocPathExpr->identificationVariable]['metadata'];
$relation = $this->_queryComponents[$joinedDqlAlias]['relation'];
$targetClass = $this->_em->getClassMetadata($relation->targetEntityName);
$sourceClass = $this->_em->getClassMetadata($relation->sourceEntityName);
$targetTableName = $targetClass->getQuotedTableName($this->_platform);
$targetTableAlias = $this->getSqlTableAlias($targetClass->getTableName(), $joinedDqlAlias);
$sourceTableAlias = $this->getSqlTableAlias(
$sourceClass->getTableName(), $joinAssocPathExpr->identificationVariable
);
$targetTableAlias = $this->getSqlTableAlias($targetClass->table['name'], $joinedDqlAlias);
$sourceTableAlias = $this->getSqlTableAlias($sourceClass->table['name'], $joinAssocPathExpr->identificationVariable);
// Ensure we got the owning side, since it has all mapping info
if ( ! $relation->isOwningSide) {
$assoc = $targetClass->associationMappings[$relation->mappedBy];
} else {
$assoc = $relation;
}
$assoc = ( ! $relation->isOwningSide) ? $targetClass->associationMappings[$relation->mappedBy] : $relation;
if ($this->_query->getHint(Query::HINT_INTERNAL_ITERATION) == true) {
if ($relation->isOneToMany() || $relation->isManyToMany()) {
......
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php';
class DDC448Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC448MainTable'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC448ConnectedClass'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC448SubTable'),
));
}
public function testIssue()
{
$q = $this->_em->createQuery("select b from ".__NAMESPACE__."\\DDC448SubTable b where b.connectedClassId = ?1");
$this->assertEquals('SELECT d0_.id AS id0, d0_.discr AS discr1, d0_.connectedClassId AS connectedClassId2 FROM SubTable s1_ INNER JOIN DDC448MainTable d0_ ON s1_.id = d0_.id WHERE d0_.connectedClassId = ?', $q->getSQL());
}
}
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="smallint")
* @DiscriminatorMap({
* "0" = "DDC448MainTable",
* "1" = "DDC448SubTable"
* })
*/
class DDC448MainTable
{
/**
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ManyToOne(targetEntity="DDC448ConnectedClass", cascade={"all"}, fetch="EAGER")
* @JoinColumn(name="connectedClassId", referencedColumnName="id", onDelete="CASCADE", onUpdate="CASCADE", nullable=true)
*/
private $connectedClassId;
}
/**
* @Entity
* @Table(name="connectedClass")
* @HasLifecycleCallbacks
*/
class DDC448ConnectedClass
{
/**
* @Id
* @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id; // connected with DDC448MainTable
}
/**
* @Entity
* @Table(name="SubTable")
*/
class DDC448SubTable extends DDC448MainTable
{
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php';
class DDC513Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC513OfferItem'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC513Item'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC513Price'),
));
}
public function testIssue()
{
$item = new DDC513OfferItem();
$this->_em->persist($item);
$this->_em->flush();
//$q = $this->_em->createQuery("select u from ".__NAMESPACE__."\\DDC513Item u left join u.price p");
//$this->assertEquals('SELECT d0_.id AS id0, d0_.discr AS discr1, d0_.price AS price2 FROM DDC513Item d0_ LEFT JOIN DDC513OfferItem d1_ ON d0_.id = d1_.id LEFT JOIN DDC513Price d2_ ON d0_.price = d2_.id', $q->getSQL());
/* THIS QUERY CAUSE EXCEPTION */
$q = $this->_em->createQuery("select u from ".__NAMESPACE__."\\DDC513OfferItem u left join u.price p");
$this->assertEquals('SELECT d0_.id AS id0, d0_.discr AS discr1, d0_.price AS price2 FROM DDC513OfferItem d1_ INNER JOIN DDC513Item d0_ ON d1_.id = d0_.id LEFT JOIN DDC513Price d2_ ON d0_.price = d2_.id', $q->getSQL());
}
}
/**
* @Entity
*/
class DDC513OfferItem extends DDC513Item
{
}
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"item" = "DDC513Item", "offerItem" = "DDC513OfferItem"})
*/
class DDC513Item
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @OneToOne(targetEntity="DDC513Price", cascade={"remove","persist"})
* @JoinColumn(name="price", referencedColumnName="id")
*/
public $price;
}
/**
* @Entity
*/
class DDC513Price {
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/** @Column(type="string") */
public $data;
}
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