2009-07-06 16:18:04 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
2009-07-28 15:43:42 +04:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2010-08-09 15:13:21 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2009-07-06 16:18:04 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a self referential many-to-many association mapping (from a model to the same model, without inheritance).
|
|
|
|
* For simplicity the relation duplicates entries in the association table
|
2013-03-11 04:08:58 +04:00
|
|
|
* to remain symmetrical.
|
2009-07-06 16:18:04 +04:00
|
|
|
*/
|
|
|
|
class ManyToManySelfReferentialAssociationTest extends AbstractManyToManyAssociationTestCase
|
|
|
|
{
|
|
|
|
protected $_firstField = 'product_id';
|
|
|
|
protected $_secondField = 'related_id';
|
|
|
|
protected $_table = 'ecommerce_products_related';
|
|
|
|
private $firstProduct;
|
|
|
|
private $secondProduct;
|
|
|
|
private $firstRelated;
|
|
|
|
private $secondRelated;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->firstProduct = new ECommerceProduct();
|
|
|
|
$this->secondProduct = new ECommerceProduct();
|
|
|
|
$this->firstRelated = new ECommerceProduct();
|
|
|
|
$this->firstRelated->setName("Business");
|
|
|
|
$this->secondRelated = new ECommerceProduct();
|
|
|
|
$this->secondRelated->setName("Home");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAManyToManyAssociationWithCascadeSaveSet()
|
|
|
|
{
|
|
|
|
$this->firstProduct->addRelated($this->firstRelated);
|
|
|
|
$this->firstProduct->addRelated($this->secondRelated);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstProduct);
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->_em->flush();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->assertForeignKeysContain($this->firstProduct->getId(),
|
|
|
|
$this->firstRelated->getId());
|
|
|
|
$this->assertForeignKeysContain($this->firstProduct->getId(),
|
|
|
|
$this->secondRelated->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesAManyToManyAssociation()
|
|
|
|
{
|
|
|
|
$this->firstProduct->addRelated($this->firstRelated);
|
|
|
|
$this->firstProduct->addRelated($this->secondRelated);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstProduct);
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->firstProduct->removeRelated($this->firstRelated);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertForeignKeysNotContain($this->firstProduct->getId(),
|
|
|
|
$this->firstRelated->getId());
|
|
|
|
$this->assertForeignKeysContain($this->firstProduct->getId(),
|
|
|
|
$this->secondRelated->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEagerLoadsOwningSide()
|
|
|
|
{
|
|
|
|
$this->_createLoadingFixture();
|
2009-07-28 15:43:42 +04:00
|
|
|
$products = $this->_findProducts();
|
2011-12-20 01:56:19 +04:00
|
|
|
$this->assertLoadingOfOwningSide($products);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLazyLoadsOwningSide()
|
|
|
|
{
|
|
|
|
$this->_createLoadingFixture();
|
|
|
|
|
|
|
|
$metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
|
2010-08-09 15:13:21 +04:00
|
|
|
$metadata->associationMappings['related']['fetch'] = ClassMetadata::FETCH_LAZY;
|
2009-07-28 15:43:42 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
|
2009-08-03 21:18:37 +04:00
|
|
|
$products = $query->getResult();
|
2011-12-20 01:56:19 +04:00
|
|
|
$this->assertLoadingOfOwningSide($products);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function assertLoadingOfOwningSide($products)
|
|
|
|
{
|
|
|
|
list ($firstProduct, $secondProduct) = $products;
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->assertEquals(2, count($firstProduct->getRelated()));
|
|
|
|
$this->assertEquals(2, count($secondProduct->getRelated()));
|
2011-12-20 01:56:19 +04:00
|
|
|
|
|
|
|
$categories = $firstProduct->getRelated();
|
2009-07-06 16:18:04 +04:00
|
|
|
$firstRelatedBy = $categories[0]->getRelated();
|
|
|
|
$secondRelatedBy = $categories[1]->getRelated();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->assertEquals(2, count($firstRelatedBy));
|
|
|
|
$this->assertEquals(2, count($secondRelatedBy));
|
|
|
|
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstRelatedBy[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstRelatedBy[1]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondRelatedBy[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondRelatedBy[1]);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->assertCollectionEquals($firstRelatedBy, $secondRelatedBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _createLoadingFixture()
|
|
|
|
{
|
|
|
|
$this->firstProduct->addRelated($this->firstRelated);
|
|
|
|
$this->firstProduct->addRelated($this->secondRelated);
|
|
|
|
$this->secondProduct->addRelated($this->firstRelated);
|
|
|
|
$this->secondProduct->addRelated($this->secondRelated);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstProduct);
|
|
|
|
$this->_em->persist($this->secondProduct);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-06 16:18:04 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _findProducts()
|
|
|
|
{
|
|
|
|
$query = $this->_em->createQuery('SELECT p, r FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.related r ORDER BY p.id, r.id');
|
2009-08-03 21:18:37 +04:00
|
|
|
return $query->getResult();
|
2009-07-06 16:18:04 +04:00
|
|
|
}
|
|
|
|
}
|