Added tests for ManyToMany associations and lazy collection to STI entity
This commit is contained in:
parent
b91689fe2f
commit
62be27b295
@ -192,7 +192,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
*/
|
*/
|
||||||
public function count(PersistentCollection $coll)
|
public function count(PersistentCollection $coll)
|
||||||
{
|
{
|
||||||
$mapping = $coll->getMapping();
|
$mapping = $filterMapping = $coll->getMapping();
|
||||||
$class = $this->_em->getClassMetadata($mapping['sourceEntity']);
|
$class = $this->_em->getClassMetadata($mapping['sourceEntity']);
|
||||||
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
|
||||||
|
|
||||||
@ -218,7 +218,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
: $id[$class->fieldNames[$joinColumns[$joinTableColumn]]];
|
: $id[$class->fieldNames[$joinColumns[$joinTableColumn]]];
|
||||||
}
|
}
|
||||||
|
|
||||||
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($mapping);
|
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping);
|
||||||
if ($filterSql) {
|
if ($filterSql) {
|
||||||
$whereClauses[] = $filterSql;
|
$whereClauses[] = $filterSql;
|
||||||
}
|
}
|
||||||
@ -295,7 +295,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters)
|
private function getJoinTableRestrictions(PersistentCollection $coll, $element, $addFilters)
|
||||||
{
|
{
|
||||||
$uow = $this->_em->getUnitOfWork();
|
$uow = $this->_em->getUnitOfWork();
|
||||||
$mapping = $coll->getMapping();
|
$mapping = $filterMapping = $coll->getMapping();
|
||||||
|
|
||||||
if ( ! $mapping['isOwningSide']) {
|
if ( ! $mapping['isOwningSide']) {
|
||||||
$sourceClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
$sourceClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||||
@ -332,7 +332,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($addFilters) {
|
if ($addFilters) {
|
||||||
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($mapping);
|
list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping);
|
||||||
if ($filterSql) {
|
if ($filterSql) {
|
||||||
$quotedJoinTable .= ' t ' . $joinTargetEntitySQL;
|
$quotedJoinTable .= ' t ' . $joinTargetEntitySQL;
|
||||||
$whereClauses[] = $filterSql;
|
$whereClauses[] = $filterSql;
|
||||||
@ -351,13 +351,21 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
* have to join in the actual entities table leading to additional
|
* have to join in the actual entities table leading to additional
|
||||||
* JOIN.
|
* JOIN.
|
||||||
*
|
*
|
||||||
* @param array $targetEntity Array containing mapping information.
|
* @param array $mapping Array containing mapping information.
|
||||||
*
|
*
|
||||||
* @return string The SQL query part to add to a query.
|
* @return string The SQL query part to add to a query.
|
||||||
*/
|
*/
|
||||||
public function getFilterSql($mapping)
|
public function getFilterSql($mapping)
|
||||||
{
|
{
|
||||||
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
||||||
|
|
||||||
|
if ($mapping['isOwningSide']) {
|
||||||
|
$joinColumns = $mapping['relationToTargetKeyColumns'];
|
||||||
|
} else {
|
||||||
|
$mapping = $targetClass->associationMappings[$mapping['mappedBy']];
|
||||||
|
$joinColumns = $mapping['relationToSourceKeyColumns'];
|
||||||
|
}
|
||||||
|
|
||||||
$targetClass = $this->_em->getClassMetadata($targetClass->rootEntityName);
|
$targetClass = $this->_em->getClassMetadata($targetClass->rootEntityName);
|
||||||
|
|
||||||
// A join is needed if there is filtering on the target entity
|
// A join is needed if there is filtering on the target entity
|
||||||
@ -368,7 +376,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
. ' ON';
|
. ' ON';
|
||||||
|
|
||||||
$joinTargetEntitySQLClauses = array();
|
$joinTargetEntitySQLClauses = array();
|
||||||
foreach ($mapping['relationToTargetKeyColumns'] as $joinTableColumn => $targetTableColumn) {
|
foreach ($joinColumns as $joinTableColumn => $targetTableColumn) {
|
||||||
$joinTargetEntitySQLClauses[] = ' t.' . $joinTableColumn . ' = ' . 'te.' . $targetTableColumn;
|
$joinTargetEntitySQLClauses[] = ' t.' . $joinTableColumn . ' = ' . 'te.' . $targetTableColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ abstract class CompanyContract
|
|||||||
private $completed = false;
|
private $completed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ManyToMany(targetEntity="CompanyEmployee")
|
* @ManyToMany(targetEntity="CompanyEmployee", inversedBy="contracts")
|
||||||
* @JoinTable(name="company_contract_employees",
|
* @JoinTable(name="company_contract_employees",
|
||||||
* joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")},
|
* joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")},
|
||||||
* inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")}
|
* inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")}
|
||||||
@ -86,4 +86,4 @@ abstract class CompanyContract
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract public function calculatePrice();
|
abstract public function calculatePrice();
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,11 @@ class CompanyEmployee extends CompanyPerson
|
|||||||
*/
|
*/
|
||||||
private $startDate;
|
private $startDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ManyToMany(targetEntity="CompanyContract", mappedBy="engineers", fetch="EXTRA_LAZY")
|
||||||
|
*/
|
||||||
|
public $contracts;
|
||||||
|
|
||||||
public function getSalary() {
|
public function getSalary() {
|
||||||
return $this->salary;
|
return $this->salary;
|
||||||
}
|
}
|
||||||
@ -46,4 +51,4 @@ class CompanyEmployee extends CompanyPerson
|
|||||||
public function setStartDate($date) {
|
public function setStartDate($date) {
|
||||||
$this->startDate = $date;
|
$this->startDate = $date;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,15 @@ class CompanyFlexContract extends CompanyContract
|
|||||||
*/
|
*/
|
||||||
private $pricePerHour = 0;
|
private $pricePerHour = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ManyToMany(targetEntity="CompanyManager", inversedBy="managedContracts")
|
||||||
|
* @JoinTable(name="company_contract_managers",
|
||||||
|
* joinColumns={@JoinColumn(name="contract_id", referencedColumnName="id", onDelete="CASCADE")},
|
||||||
|
* inverseJoinColumns={@JoinColumn(name="employee_id", referencedColumnName="id")}
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
private $managers;
|
||||||
|
|
||||||
public function calculatePrice()
|
public function calculatePrice()
|
||||||
{
|
{
|
||||||
return $this->hoursWorked * $this->pricePerHour;
|
return $this->hoursWorked * $this->pricePerHour;
|
||||||
@ -42,4 +51,18 @@ class CompanyFlexContract extends CompanyContract
|
|||||||
{
|
{
|
||||||
$this->pricePerHour = $pricePerHour;
|
$this->pricePerHour = $pricePerHour;
|
||||||
}
|
}
|
||||||
}
|
public function getManagers()
|
||||||
|
{
|
||||||
|
return $this->managers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addManager(CompanyManager $manager)
|
||||||
|
{
|
||||||
|
$this->managers[] = $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeManager(CompanyManager $manager)
|
||||||
|
{
|
||||||
|
$this->managers->removeElement($manager);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -19,6 +19,11 @@ class CompanyManager extends CompanyEmployee
|
|||||||
*/
|
*/
|
||||||
private $car;
|
private $car;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ManyToMany(targetEntity="CompanyFlexContract", mappedBy="managers", fetch="EXTRA_LAZY")
|
||||||
|
*/
|
||||||
|
public $managedContracts;
|
||||||
|
|
||||||
public function getTitle() {
|
public function getTitle() {
|
||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
@ -34,4 +39,4 @@ class CompanyManager extends CompanyEmployee
|
|||||||
public function setCar(CompanyCar $car) {
|
public function setCar(CompanyCar $car) {
|
||||||
$this->car = $car;
|
$this->car = $car;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
{
|
{
|
||||||
private $userId, $userId2, $articleId, $articleId2;
|
private $userId, $userId2, $articleId, $articleId2;
|
||||||
private $groupId, $groupId2;
|
private $groupId, $groupId2;
|
||||||
|
private $managerId, $contractId1, $contractId2;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
@ -655,12 +656,89 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$contract4 = new CompanyFlexContract;
|
$contract4 = new CompanyFlexContract;
|
||||||
$contract4->markCompleted();
|
$contract4->markCompleted();
|
||||||
|
|
||||||
|
$manager = new CompanyManager;
|
||||||
|
$manager->setName('Alexander');
|
||||||
|
$manager->setSalary(42);
|
||||||
|
$manager->setDepartment('Doctrine');
|
||||||
|
$manager->setTitle('Filterer');
|
||||||
|
|
||||||
|
$contract1->addManager($manager);
|
||||||
|
$contract2->addManager($manager);
|
||||||
|
$contract3->addManager($manager);
|
||||||
|
$contract4->addManager($manager);
|
||||||
|
|
||||||
|
$this->_em->persist($manager);
|
||||||
$this->_em->persist($contract1);
|
$this->_em->persist($contract1);
|
||||||
$this->_em->persist($contract2);
|
$this->_em->persist($contract2);
|
||||||
$this->_em->persist($contract3);
|
$this->_em->persist($contract3);
|
||||||
$this->_em->persist($contract4);
|
$this->_em->persist($contract4);
|
||||||
$this->_em->flush();
|
$this->_em->flush();
|
||||||
$this->_em->clear();
|
$this->_em->clear();
|
||||||
|
|
||||||
|
$this->managerId = $manager->getId();
|
||||||
|
$this->contractId1 = $contract1->getId();
|
||||||
|
$this->contractId2 = $contract2->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function useCompletedContractFilter()
|
||||||
|
{
|
||||||
|
$conf = $this->_em->getConfiguration();
|
||||||
|
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
|
||||||
|
$this->_em->getFilters()
|
||||||
|
->enable("completed_contract")
|
||||||
|
->setParameter("completed", true, DBALType::BOOLEAN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testManyToMany_ExtraLazyCountWithFilterOnSTI()
|
||||||
|
{
|
||||||
|
$this->loadCompanySingleTableInheritanceFixtureData();
|
||||||
|
|
||||||
|
$manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId);
|
||||||
|
|
||||||
|
$this->assertFalse($manager->managedContracts->isInitialized());
|
||||||
|
$this->assertEquals(4, count($manager->managedContracts));
|
||||||
|
|
||||||
|
// Enable the filter
|
||||||
|
$this->useCompletedContractFilter();
|
||||||
|
|
||||||
|
$this->assertFalse($manager->managedContracts->isInitialized());
|
||||||
|
$this->assertEquals(2, count($manager->managedContracts));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testManyToMany_ExtraLazyContainsWithFilterOnSTI()
|
||||||
|
{
|
||||||
|
$this->loadCompanySingleTableInheritanceFixtureData();
|
||||||
|
|
||||||
|
$manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId);
|
||||||
|
$contract1 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyContract', $this->contractId1);
|
||||||
|
$contract2 = $this->_em->find('Doctrine\Tests\Models\Company\CompanyContract', $this->contractId2);
|
||||||
|
|
||||||
|
$this->assertFalse($manager->managedContracts->isInitialized());
|
||||||
|
$this->assertTrue($manager->managedContracts->contains($contract1));
|
||||||
|
$this->assertTrue($manager->managedContracts->contains($contract2));
|
||||||
|
|
||||||
|
// Enable the filter
|
||||||
|
$this->useCompletedContractFilter();
|
||||||
|
|
||||||
|
$this->assertFalse($manager->managedContracts->isInitialized());
|
||||||
|
$this->assertFalse($manager->managedContracts->contains($contract1));
|
||||||
|
$this->assertTrue($manager->managedContracts->contains($contract2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testManyToMany_ExtraLazySliceWithFilterOnSTI()
|
||||||
|
{
|
||||||
|
$this->loadCompanySingleTableInheritanceFixtureData();
|
||||||
|
|
||||||
|
$manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $this->managerId);
|
||||||
|
|
||||||
|
$this->assertFalse($manager->managedContracts->isInitialized());
|
||||||
|
$this->assertEquals(4, count($manager->managedContracts->slice(0, 10)));
|
||||||
|
|
||||||
|
// Enable the filter
|
||||||
|
$this->useCompletedContractFilter();
|
||||||
|
|
||||||
|
$this->assertFalse($manager->managedContracts->isInitialized());
|
||||||
|
$this->assertEquals(2, count($manager->managedContracts->slice(0, 10)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user