-
-
Save doctrinebot/b63fcda4b9538b5daf42 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-61 - https://github.com/doctrine/doctrine2/issues/5118
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111# svn diff CompanyOrganization.php | |
Index: CompanyOrganization.php | |
=================================================================== | |
--- CompanyOrganization.php (revision 6574) | |
+++ CompanyOrganization.php (working copy) | |
@@ -15,6 +15,11 @@ | |
*/ | |
private $events; | |
+ /** | |
+ * @OneToOne(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"}) | |
+ */ | |
+ private $mainevent; | |
+ | |
public function getId() { | |
return $this->id; | |
} | |
@@ -27,4 +32,14 @@ | |
$this->events[] = $event; | |
$event->setOrganization($this); | |
} | |
+ | |
+ public function setMainEvent($event) | |
+ { | |
+ $this->mainevent = $event; | |
+ } | |
+ | |
+ public function getMainEvent() | |
+ { | |
+ return $this->mainevent; | |
+ } | |
} | |
117# svn diff ClassTableInheritanceTest.php | |
Index: ClassTableInheritanceTest.php | |
=================================================================== | |
--- ClassTableInheritanceTest.php (revision 6574) | |
+++ ClassTableInheritanceTest.php (working copy) | |
@@ -238,4 +238,38 @@ | |
$this->assertTrue($events[1] instanceof CompanyAuction); | |
} | |
} | |
+ | |
+ public function testLazyLoading2() | |
+ { | |
+ $org = new CompanyOrganization; | |
+ $event1 = new CompanyAuction; | |
+ $event1->setData('auction'); | |
+ $org->setMainEvent($event1); | |
+ | |
+ $this->_em->persist($org); | |
+ $this->_em->flush(); | |
+ $this->_em->clear(); | |
+ | |
+ $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyEvent a where a.id = ?1'); | |
+ $q->setParameter(1, $event1->getId()); | |
+ | |
+ $result = $q->getResult(); | |
+ $this->assertEquals(1, count($result)); | |
+ $this->assertTrue($result[0] instanceof CompanyAuction,sprintf("Is of class %s",get_class($result[0]))); | |
+ | |
+ $this->_em->clear(); | |
+ | |
+ $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1'); | |
+ $q->setParameter(1, $org->getId()); | |
+ | |
+ $result = $q->getResult(); | |
+ | |
+ $this->assertEquals(1, count($result)); | |
+ $this->assertTrue($result[0] instanceof CompanyOrganization); | |
+ | |
+ $mainEvent = $result[0]->getMainEvent(); | |
+ | |
+ $this->assertTrue($mainEvent instanceof CompanyAuction,sprintf("Is of class %s",get_class($mainEvent))); | |
+ | |
+ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111# svn diff CompanyOrganization.php | |
Index: CompanyOrganization.php | |
=================================================================== | |
--- CompanyOrganization.php (revision 6574) | |
+++ CompanyOrganization.php (working copy) | |
@@ -15,6 +15,11 @@ | |
*/ | |
private $events; | |
+ /** | |
+ * @OneToOne(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"}) | |
+ */ | |
+ private $mainevent; | |
+ | |
public function getId() { | |
return $this->id; | |
} | |
@@ -27,4 +32,15 @@ | |
$this->events[] = $event; | |
$event->setOrganization($this); | |
} | |
+ | |
+ public function setMainEvent($event) | |
+ { | |
+ $event->setOrganization($this); | |
+ $this->mainevent = $event; | |
+ } | |
+ | |
+ public function getMainEvent() | |
+ { | |
+ return $this->mainevent; | |
+ } | |
} | |
117# svn diff ClassTableInheritanceTest.php | |
Index: ClassTableInheritanceTest.php | |
=================================================================== | |
--- ClassTableInheritanceTest.php (revision 6574) | |
+++ ClassTableInheritanceTest.php (working copy) | |
@@ -238,4 +238,38 @@ | |
$this->assertTrue($events[1] instanceof CompanyAuction); | |
} | |
} | |
+ | |
+ public function testLazyLoading2() | |
+ { | |
+ $org = new CompanyOrganization; | |
+ $event1 = new CompanyAuction; | |
+ $event1->setData('auction'); | |
+ $org->setMainEvent($event1); | |
+ | |
+ $this->_em->persist($org); | |
+ $this->_em->flush(); | |
+ $this->_em->clear(); | |
+ | |
+ $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyEvent a where a.id = ?1'); | |
+ $q->setParameter(1, $event1->getId()); | |
+ | |
+ $result = $q->getResult(); | |
+ $this->assertEquals(1, count($result)); | |
+ $this->assertTrue($result[0] instanceof CompanyAuction,sprintf("Is of class %s",get_class($result[0]))); | |
+ | |
+ $this->_em->clear(); | |
+ | |
+ $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1'); | |
+ $q->setParameter(1, $org->getId()); | |
+ | |
+ $result = $q->getResult(); | |
+ | |
+ $this->assertEquals(1, count($result)); | |
+ $this->assertTrue($result[0] instanceof CompanyOrganization); | |
+ | |
+ $mainEvent = $result[0]->getMainEvent(); | |
+ | |
+ $this->assertTrue($mainEvent instanceof CompanyAuction,sprintf("Is of class %s",get_class($mainEvent))); | |
+ | |
+ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: lib/Doctrine/ORM/Persisters/StandardEntityPersister.php | |
=================================================================== | |
--- lib/Doctrine/ORM/Persisters/StandardEntityPersister.php (revision 6587) | |
+++ lib/Doctrine/ORM/Persisters/StandardEntityPersister.php (working copy) | |
@@ -565,25 +565,29 @@ | |
if ($result === false) { | |
return null; | |
} | |
- | |
+ $class = $this->_class; | |
$data = $joinColumnValues = array(); | |
$entityName = $this->_entityName; | |
- | |
+ $hints = array(); | |
foreach ($result as $column => $value) { | |
- $column = $this->_class->resultColumnNames[$column]; | |
+ | |
+ $column = isset($this->_class->resultColumnNames[$column]) ? $this->_class->resultColumnNames[$column] : $column; | |
if (isset($this->_class->fieldNames[$column])) { | |
$fieldName = $this->_class->fieldNames[$column]; | |
$data[$fieldName] = Type::getType($this->_class->fieldMappings[$fieldName]['type']) | |
->convertToPHPValue($value, $this->_platform); | |
} else if ($this->_class->discriminatorColumn !== null && $column == $this->_class->discriminatorColumn['name']) { | |
$entityName = $this->_class->discriminatorMap[$value]; | |
+ } else if (isset($this->_class->associationMappings[$column])) { | |
+ // $column == $sourceFieldName | |
+ $hints["associations"][$column] = $value; | |
} else { | |
$data[$column] = $value; | |
$joinColumnValues[$column] = $value; | |
} | |
} | |
- $hints = array(); | |
+ | |
if ($entity !== null) { | |
$hints[Query::HINT_REFRESH] = true; | |
@@ -609,45 +613,65 @@ | |
*/ | |
protected function _getSelectEntitiesSql(array &$criteria, $assoc = null) | |
{ | |
- $columnList = ''; | |
+ $columns = array(); | |
+ $tables = array($this->_class->getQuotedTableName($this->_platform)); | |
// Add regular columns to select list | |
foreach ($this->_class->fieldNames as $field) { | |
- if ($columnList != '') $columnList .= ', '; | |
- $columnList .= $this->_class->getQuotedColumnName($field, $this->_platform); | |
+ $columns[] = sprintf("%s.%s", | |
+ $tables[0], | |
+ $this->_class->getQuotedColumnName($field, $this->_platform) | |
+ ); | |
} | |
- | |
+ | |
// Add join columns (foreign keys) to select list | |
foreach ($this->_class->associationMappings as $assoc2) { | |
- if ($assoc2->isOwningSide && $assoc2->isOneToOne()) { | |
- foreach ($assoc2->targetToSourceKeyColumns as $srcColumn) { | |
- $columnList .= ', ' . $assoc2->getQuotedJoinColumnName($srcColumn, $this->_platform); | |
+ | |
+ // Join associations to inheritance targets to check if one exists | |
+ // and find out target final type (if uses table inheritance) | |
+ if ($assoc2->isOneToOne()) { | |
+ if (! $assoc2->isOwningSide) { | |
+ $target = $this->_em->getClassMetadata($assoc2->targetEntityName); | |
+ $owningAssoc = $target->getAssociationMapping($assoc2->mappedByFieldName); | |
+ $tTable = $target->getQuotedTableName($this->_platform); | |
+ $jCond = array(); | |
+ foreach ($owningAssoc->getJoinColumns() as $jCol) { | |
+ $jCond[] = sprintf("%s.%s = %s.%s", $tables[0], $jCol['referencedColumnName'], $tTable, $jCol['name']); | |
+ } | |
+ if ($target->inheritanceType === \Doctrine\ORM\Mapping\ClassMetadataInfo::INHERITANCE_TYPE_NONE) { | |
+ $idCol = reset($target->getIdentifierColumnNames()); | |
+ $columns[] = sprintf("%s.%s AS %s", $tTable, $idCol, $assoc2->sourceFieldName); | |
+ } else { | |
+ $columns[] = sprintf("%s.%s AS %s", $tTable, $target->getQuotedDiscriminatorColumnName($this->_platform), $assoc2->sourceFieldName); | |
+ } | |
+ $tables[] = sprintf("%s ON (%s)", $tTable, implode(' AND ', $jCond)); | |
+ | |
+ } else { | |
+ foreach ($assoc2->targetToSourceKeyColumns as $srcColumn) { | |
+ $columns[] = sprintf("%s.%s", $tables[0], $assoc2->getQuotedJoinColumnName($srcColumn, $this->_platform)); | |
+ } | |
} | |
} | |
} | |
// Construct WHERE conditions | |
- $conditionSql = ''; | |
+ $conditions = array(); | |
foreach ($criteria as $field => $value) { | |
- if ($conditionSql != '') { | |
- $conditionSql .= ' AND '; | |
- } | |
- | |
if (isset($this->_class->columnNames[$field])) { | |
- $conditionSql .= $this->_class->getQuotedColumnName($field, $this->_platform); | |
+ $qField = $this->_class->getQuotedColumnName($field, $this->_platform); | |
} else if (isset($this->_class->fieldNames[$field])) { | |
- $conditionSql .= $this->_class->getQuotedColumnName($this->_class->fieldNames[$field], $this->_platform); | |
+ $qField = $this->_class->getQuotedColumnName($this->_class->fieldNames[$field], $this->_platform); | |
} else if ($assoc !== null) { | |
- $conditionSql .= $assoc->getQuotedJoinColumnName($field, $this->_platform); | |
+ $qField = $assoc->getQuotedJoinColumnName($field, $this->_platform); | |
} else { | |
throw ORMException::unrecognizedField($field); | |
} | |
- $conditionSql .= ' = ?'; | |
+ $conditions[] = sprintf("%s.%s = ?",$tables[0],$qField); | |
} | |
- return 'SELECT ' . $columnList | |
- . ' FROM ' . $this->_class->getQuotedTableName($this->_platform) | |
- . ($conditionSql ? ' WHERE ' . $conditionSql : ''); | |
+ return 'SELECT ' . implode(', ',$columns) | |
+ . ' FROM ' . implode(' LEFT JOIN ',$tables) | |
+ . ($conditions ? ' WHERE ' . implode(' AND ',$conditions) : ''); | |
} | |
/** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment