From 3d3bcc1742dd31b16e56f984a430e1b60d586da1 Mon Sep 17 00:00:00 2001 From: romanb Date: Tue, 15 Sep 2009 12:24:38 +0000 Subject: [PATCH] [2.0][DDC-7] Fixed. --- lib/Doctrine/ORM/PersistentCollection.php | 1 - .../Persisters/JoinedSubclassPersister.php | 15 ++++-- .../Persisters/StandardEntityPersister.php | 17 +++--- .../Tests/Models/Company/CompanyAuction.php | 17 ++++++ .../Tests/Models/Company/CompanyEvent.php | 35 +++++++++++++ .../Models/Company/CompanyOrganization.php | 30 +++++++++++ .../Tests/Models/Company/CompanyRaffle.php | 17 ++++++ .../Functional/ClassTableInheritanceTest.php | 52 +++++++++++++++++-- .../Doctrine/Tests/OrmFunctionalTestCase.php | 10 +++- 9 files changed, 177 insertions(+), 17 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/Company/CompanyAuction.php create mode 100644 tests/Doctrine/Tests/Models/Company/CompanyEvent.php create mode 100644 tests/Doctrine/Tests/Models/Company/CompanyOrganization.php create mode 100644 tests/Doctrine/Tests/Models/Company/CompanyRaffle.php diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 5eb139628..4a16780f4 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -294,7 +294,6 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect { return $this->_association; } - /** * Marks this collection as changed/dirty. diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 7da15eae6..32f49c3bf 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -269,10 +269,9 @@ class JoinedSubclassPersister extends StandardEntityPersister * * @param array $criteria * @return string The SQL. - * @todo Quote identifier. * @override */ - protected function _getSelectEntitiesSql(array &$criteria) + protected function _getSelectEntitiesSql(array &$criteria, $assoc = null) { $tableAliases = array(); $aliasIndex = 1; @@ -286,7 +285,7 @@ class JoinedSubclassPersister extends StandardEntityPersister $columnList = ''; foreach ($this->_class->fieldMappings as $fieldName => $mapping) { $tableAlias = isset($mapping['inherited']) ? - $tableAliases[$mapping['inherited']] : $baseTableAlias; + $tableAliases[$mapping['inherited']] : $baseTableAlias; if ($columnList != '') $columnList .= ', '; $columnList .= $tableAlias . '.' . $this->_class->getQuotedColumnName($fieldName, $this->_platform); } @@ -329,7 +328,15 @@ class JoinedSubclassPersister extends StandardEntityPersister $conditionSql = ''; foreach ($criteria as $field => $value) { if ($conditionSql != '') $conditionSql .= ' AND '; - $conditionSql .= $baseTableAlias . '.' . $this->_class->columnNames[$field] . ' = ?'; + $conditionSql .= $baseTableAlias . '.'; + if (isset($this->_class->columnNames[$field])) { + $conditionSql .= $this->_class->getQuotedColumnName($field, $this->_platform); + } else if ($assoc !== null) { + $conditionSql .= $assoc->getQuotedJoinColumnName($field, $this->_platform); + } else { + throw DoctrineException::unrecognizedField($field); + } + $conditionSql .= ' = ?'; } return $sql . ($conditionSql != '' ? ' WHERE ' . $conditionSql : ''); diff --git a/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php b/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php index a5489919e..c7cfea833 100644 --- a/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php @@ -452,12 +452,13 @@ class StandardEntityPersister * @param array $criteria The criteria by which to select the entities. * @param PersistentCollection The collection to fill. */ - public function loadOneToManyCollection(array $criteria, PersistentCollection $collection) + public function loadOneToManyCollection(array $criteria, PersistentCollection $coll) { - $stmt = $this->_conn->prepare($this->_getSelectEntitiesSql($criteria)); + $owningAssoc = $this->_class->associationMappings[$coll->getMapping()->mappedByFieldName]; + $stmt = $this->_conn->prepare($this->_getSelectEntitiesSql($criteria, $owningAssoc)); $stmt->execute(array_values($criteria)); while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) { - $collection->hydrateAdd($this->_createEntity($result)); + $coll->hydrateAdd($this->_createEntity($result)); } $stmt->closeCursor(); } @@ -565,7 +566,7 @@ class StandardEntityPersister * @param array $criteria * @return string The SQL. */ - protected function _getSelectEntitiesSql(array &$criteria) + protected function _getSelectEntitiesSql(array &$criteria, $assoc = null) { $columnList = ''; foreach ($this->_class->fieldNames as $field) { @@ -593,13 +594,13 @@ class StandardEntityPersister } if (isset($this->_class->columnNames[$field])) { - $columnName = $this->_class->getQuotedColumnName($field, $this->_platform); - } else if (in_array($field, $joinColumnNames)) { - $columnName = $field; + $conditionSql .= $this->_class->getQuotedColumnName($field, $this->_platform); + } else if ($assoc !== null) { + $conditionSql .= $assoc->getQuotedJoinColumnName($field, $this->_platform); } else { throw DoctrineException::unrecognizedField($field); } - $conditionSql .= $columnName . ' = ?'; + $conditionSql .= ' = ?'; } return 'SELECT ' . $columnList diff --git a/tests/Doctrine/Tests/Models/Company/CompanyAuction.php b/tests/Doctrine/Tests/Models/Company/CompanyAuction.php new file mode 100644 index 000000000..88b56e2f6 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Company/CompanyAuction.php @@ -0,0 +1,17 @@ +data = $data; + } + + public function getData() { + return $this->data; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/Company/CompanyEvent.php b/tests/Doctrine/Tests/Models/Company/CompanyEvent.php new file mode 100644 index 000000000..91a9ddb7f --- /dev/null +++ b/tests/Doctrine/Tests/Models/Company/CompanyEvent.php @@ -0,0 +1,35 @@ +id; + } + + public function getOrganization() { + return $this->organization; + } + + public function setOrganization(CompanyOrganization $org) { + $this->organization = $org; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php b/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php new file mode 100644 index 000000000..15237274a --- /dev/null +++ b/tests/Doctrine/Tests/Models/Company/CompanyOrganization.php @@ -0,0 +1,30 @@ +id; + } + + public function getEvents() { + return $this->events; + } + + public function addEvent(CompanyEvent $event) { + $this->events[] = $event; + $event->setOrganization($this); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/Company/CompanyRaffle.php b/tests/Doctrine/Tests/Models/Company/CompanyRaffle.php new file mode 100644 index 000000000..a7d123c84 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Company/CompanyRaffle.php @@ -0,0 +1,17 @@ +data = $data; + } + + public function getData() { + return $this->data; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php index 0ea6f9e07..376e764c5 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php @@ -4,9 +4,13 @@ namespace Doctrine\Tests\ORM\Functional; require_once __DIR__ . '/../../TestInit.php'; -use Doctrine\Tests\Models\Company\CompanyPerson; -use Doctrine\Tests\Models\Company\CompanyEmployee; -use Doctrine\Tests\Models\Company\CompanyManager; +use Doctrine\Tests\Models\Company\CompanyPerson, + Doctrine\Tests\Models\Company\CompanyEmployee, + Doctrine\Tests\Models\Company\CompanyManager, + Doctrine\Tests\Models\Company\CompanyOrganization, + Doctrine\Tests\Models\Company\CompanyEvent, + Doctrine\Tests\Models\Company\CompanyAuction, + Doctrine\Tests\Models\Company\CompanyRaffle; /** * Functional tests for the Class Table Inheritance mapping strategy. @@ -176,4 +180,46 @@ class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase $friends = $result[0]->getFriends(); $this->assertEquals('Jonathan', $friends[0]->getName()); } + + public function testLazyLoading1() + { + $org = new CompanyOrganization; + $event1 = new CompanyAuction; + $event1->setData('auction'); + $org->addEvent($event1); + $event2 = new CompanyRaffle; + $event2->setData('raffle'); + $org->addEvent($event2); + + $this->_em->persist($org); + $this->_em->flush(); + $this->_em->clear(); + + $orgId = $org->getId(); + + $this->_em->getConfiguration()->setAllowPartialObjects(false); + + $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1'); + $q->setParameter(1, $orgId); + + $result = $q->getResult(); + + $this->assertEquals(1, count($result)); + $this->assertTrue($result[0] instanceof CompanyOrganization); + + $events = $result[0]->getEvents(); + + $this->assertTrue($events instanceof \Doctrine\ORM\PersistentCollection); + $this->assertFalse($events->isInitialized()); + + $this->assertEquals(2, count($events)); + if ($events[0] instanceof CompanyAuction) { + $this->assertTrue($events[1] instanceof CompanyRaffle); + } else { + $this->assertTrue($events[0] instanceof CompanyRaffle); + $this->assertTrue($events[1] instanceof CompanyAuction); + } + + $this->_em->getConfiguration()->setAllowPartialObjects(true); + } } diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index ba4101703..95dfb31a0 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -42,7 +42,11 @@ class OrmFunctionalTestCase extends OrmTestCase 'company' => array( 'Doctrine\Tests\Models\Company\CompanyPerson', 'Doctrine\Tests\Models\Company\CompanyEmployee', - 'Doctrine\Tests\Models\Company\CompanyManager' + 'Doctrine\Tests\Models\Company\CompanyManager', + 'Doctrine\Tests\Models\Company\CompanyOrganization', + 'Doctrine\Tests\Models\Company\CompanyEvent', + 'Doctrine\Tests\Models\Company\CompanyAuction', + 'Doctrine\Tests\Models\Company\CompanyRaffle' ), 'ecommerce' => array( 'Doctrine\Tests\Models\ECommerce\ECommerceCart', @@ -94,6 +98,10 @@ class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM company_employees'); $conn->executeUpdate('UPDATE company_persons SET spouse_id = NULL'); $conn->executeUpdate('DELETE FROM company_persons'); + $conn->executeUpdate('DELETE FROM company_raffles'); + $conn->executeUpdate('DELETE FROM company_auctions'); + $conn->executeUpdate('DELETE FROM company_events'); + $conn->executeUpdate('DELETE FROM company_organizations'); } if (isset($this->_usedModelSets['generic'])) { $conn->executeUpdate('DELETE FROM date_time_model');