Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[2.0][DDC-448][DDC-513] Fixed issue with Joined Inheritance Type and …

…One To One Associations.
  • Loading branch information...
commit 56a8f5cd5353908b815607a6e089201c95e01e6c 1 parent b6f9cd0
Guilherme Blanco authored
View
31 lib/Doctrine/ORM/Query/SqlWalker.php
@@ -465,6 +465,11 @@ public function walkPathExpression($pathExpr)
$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,8 +477,8 @@ public function walkPathExpression($pathExpr)
if (count($assoc->sourceToTargetKeyColumns) > 1) {
throw QueryException::associationPathCompositeKeyNotSupported();
}
- $sql .= $this->walkIdentificationVariable($dqlAlias) . '.'
- . reset($assoc->targetToSourceKeyColumns);
+ $sql .= $this->getSqlTableAlias($class->table['name'], $dqlAlias) . '.'
+ . reset($assoc->targetToSourceKeyColumns);
} else {
// 2- Inverse side: NOT (YET?) SUPPORTED
throw QueryException::associationPathInverseSideNotSupported();
@@ -683,23 +688,15 @@ public function walkJoinVariableDeclaration($joinVarDecl)
$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()) {
@@ -713,7 +710,7 @@ public function walkJoinVariableDeclaration($joinVarDecl)
foreach ($assoc->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) {
if ( ! $first) $sql .= ' AND '; else $first = false;
-
+
if ($relation->isOwningSide) {
$quotedTargetColumn = $targetClass->getQuotedColumnName($targetClass->fieldNames[$targetColumn], $this->_platform);
$sql .= $sourceTableAlias . '.' . $sourceColumn
View
71 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC448Test.php
@@ -0,0 +1,71 @@
+<?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
+{
+}
View
79 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC513Test.php
@@ -0,0 +1,79 @@
+<?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;
+}
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.