1
0
mirror of synced 2025-01-22 08:11:40 +03:00

[2.0][DDC-7] Fixed.

This commit is contained in:
romanb 2009-09-15 12:24:38 +00:00
parent 7ef91a6449
commit 3d3bcc1742
9 changed files with 177 additions and 17 deletions

View File

@ -295,7 +295,6 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
return $this->_association; return $this->_association;
} }
/** /**
* Marks this collection as changed/dirty. * Marks this collection as changed/dirty.
*/ */

View File

@ -269,10 +269,9 @@ class JoinedSubclassPersister extends StandardEntityPersister
* *
* @param array $criteria * @param array $criteria
* @return string The SQL. * @return string The SQL.
* @todo Quote identifier.
* @override * @override
*/ */
protected function _getSelectEntitiesSql(array &$criteria) protected function _getSelectEntitiesSql(array &$criteria, $assoc = null)
{ {
$tableAliases = array(); $tableAliases = array();
$aliasIndex = 1; $aliasIndex = 1;
@ -286,7 +285,7 @@ class JoinedSubclassPersister extends StandardEntityPersister
$columnList = ''; $columnList = '';
foreach ($this->_class->fieldMappings as $fieldName => $mapping) { foreach ($this->_class->fieldMappings as $fieldName => $mapping) {
$tableAlias = isset($mapping['inherited']) ? $tableAlias = isset($mapping['inherited']) ?
$tableAliases[$mapping['inherited']] : $baseTableAlias; $tableAliases[$mapping['inherited']] : $baseTableAlias;
if ($columnList != '') $columnList .= ', '; if ($columnList != '') $columnList .= ', ';
$columnList .= $tableAlias . '.' . $this->_class->getQuotedColumnName($fieldName, $this->_platform); $columnList .= $tableAlias . '.' . $this->_class->getQuotedColumnName($fieldName, $this->_platform);
} }
@ -329,7 +328,15 @@ class JoinedSubclassPersister extends StandardEntityPersister
$conditionSql = ''; $conditionSql = '';
foreach ($criteria as $field => $value) { foreach ($criteria as $field => $value) {
if ($conditionSql != '') $conditionSql .= ' AND '; 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 : ''); return $sql . ($conditionSql != '' ? ' WHERE ' . $conditionSql : '');

View File

@ -452,12 +452,13 @@ class StandardEntityPersister
* @param array $criteria The criteria by which to select the entities. * @param array $criteria The criteria by which to select the entities.
* @param PersistentCollection The collection to fill. * @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)); $stmt->execute(array_values($criteria));
while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) { while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) {
$collection->hydrateAdd($this->_createEntity($result)); $coll->hydrateAdd($this->_createEntity($result));
} }
$stmt->closeCursor(); $stmt->closeCursor();
} }
@ -565,7 +566,7 @@ class StandardEntityPersister
* @param array $criteria * @param array $criteria
* @return string The SQL. * @return string The SQL.
*/ */
protected function _getSelectEntitiesSql(array &$criteria) protected function _getSelectEntitiesSql(array &$criteria, $assoc = null)
{ {
$columnList = ''; $columnList = '';
foreach ($this->_class->fieldNames as $field) { foreach ($this->_class->fieldNames as $field) {
@ -593,13 +594,13 @@ class StandardEntityPersister
} }
if (isset($this->_class->columnNames[$field])) { if (isset($this->_class->columnNames[$field])) {
$columnName = $this->_class->getQuotedColumnName($field, $this->_platform); $conditionSql .= $this->_class->getQuotedColumnName($field, $this->_platform);
} else if (in_array($field, $joinColumnNames)) { } else if ($assoc !== null) {
$columnName = $field; $conditionSql .= $assoc->getQuotedJoinColumnName($field, $this->_platform);
} else { } else {
throw DoctrineException::unrecognizedField($field); throw DoctrineException::unrecognizedField($field);
} }
$conditionSql .= $columnName . ' = ?'; $conditionSql .= ' = ?';
} }
return 'SELECT ' . $columnList return 'SELECT ' . $columnList

View File

@ -0,0 +1,17 @@
<?php
namespace Doctrine\Tests\Models\Company;
/** @Entity @Table(name="company_auctions") */
class CompanyAuction extends CompanyEvent {
/** @Column(type="string") */
private $data;
public function setData($data) {
$this->data = $data;
}
public function getData() {
return $this->data;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Doctrine\Tests\Models\Company;
/**
* @Entity @Table(name="company_events")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="event_type", type="string")
* @DiscriminatorMap({"auction" = "CompanyAuction", "raffle" = "CompanyRaffle"})
*/
class CompanyEvent {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @OneToOne(targetEntity="CompanyOrganization",cascade={"persist"})
* @JoinColumn(name="org_id", referencedColumnName="id")
*/
private $organization;
public function getId() {
return $this->id;
}
public function getOrganization() {
return $this->organization;
}
public function setOrganization(CompanyOrganization $org) {
$this->organization = $org;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Doctrine\Tests\Models\Company;
/** @Entity @Table(name="company_organizations") */
class CompanyOrganization {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @OneToMany(targetEntity="CompanyEvent", mappedBy="organization", cascade={"persist"})
*/
private $events;
public function getId() {
return $this->id;
}
public function getEvents() {
return $this->events;
}
public function addEvent(CompanyEvent $event) {
$this->events[] = $event;
$event->setOrganization($this);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Doctrine\Tests\Models\Company;
/** @Entity @Table(name="company_raffles") */
class CompanyRaffle extends CompanyEvent {
/** @Column(type="string") */
private $data;
public function setData($data) {
$this->data = $data;
}
public function getData() {
return $this->data;
}
}

View File

@ -4,9 +4,13 @@ namespace Doctrine\Tests\ORM\Functional;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
use Doctrine\Tests\Models\Company\CompanyPerson; use Doctrine\Tests\Models\Company\CompanyPerson,
use Doctrine\Tests\Models\Company\CompanyEmployee; Doctrine\Tests\Models\Company\CompanyEmployee,
use 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;
/** /**
* Functional tests for the Class Table Inheritance mapping strategy. * Functional tests for the Class Table Inheritance mapping strategy.
@ -176,4 +180,46 @@ class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase
$friends = $result[0]->getFriends(); $friends = $result[0]->getFriends();
$this->assertEquals('Jonathan', $friends[0]->getName()); $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);
}
} }

View File

@ -42,7 +42,11 @@ class OrmFunctionalTestCase extends OrmTestCase
'company' => array( 'company' => array(
'Doctrine\Tests\Models\Company\CompanyPerson', 'Doctrine\Tests\Models\Company\CompanyPerson',
'Doctrine\Tests\Models\Company\CompanyEmployee', '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( 'ecommerce' => array(
'Doctrine\Tests\Models\ECommerce\ECommerceCart', 'Doctrine\Tests\Models\ECommerce\ECommerceCart',
@ -94,6 +98,10 @@ class OrmFunctionalTestCase extends OrmTestCase
$conn->executeUpdate('DELETE FROM company_employees'); $conn->executeUpdate('DELETE FROM company_employees');
$conn->executeUpdate('UPDATE company_persons SET spouse_id = NULL'); $conn->executeUpdate('UPDATE company_persons SET spouse_id = NULL');
$conn->executeUpdate('DELETE FROM company_persons'); $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'])) { if (isset($this->_usedModelSets['generic'])) {
$conn->executeUpdate('DELETE FROM date_time_model'); $conn->executeUpdate('DELETE FROM date_time_model');