2009-07-02 13:37:59 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
|
2009-07-28 15:43:42 +04:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2009-10-15 18:39:43 +04:00
|
|
|
use Doctrine\ORM\Query;
|
2009-07-02 13:37:59 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a bidirectional many-to-many association mapping (without inheritance).
|
2009-07-28 15:43:42 +04:00
|
|
|
* Owning side is ECommerceProduct, inverse side is ECommerceCategory.
|
2009-07-02 13:37:59 +04:00
|
|
|
*/
|
|
|
|
class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
|
|
|
|
{
|
2009-07-02 15:48:44 +04:00
|
|
|
protected $_firstField = 'product_id';
|
|
|
|
protected $_secondField = 'category_id';
|
|
|
|
protected $_table = 'ecommerce_products_categories';
|
2009-07-02 13:37:59 +04:00
|
|
|
private $firstProduct;
|
|
|
|
private $secondProduct;
|
|
|
|
private $firstCategory;
|
|
|
|
private $secondCategory;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->firstProduct = new ECommerceProduct();
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->firstProduct->setName("First Product");
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->secondProduct = new ECommerceProduct();
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->secondProduct->setName("Second Product");
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->firstCategory = new ECommerceCategory();
|
|
|
|
$this->firstCategory->setName("Business");
|
|
|
|
$this->secondCategory = new ECommerceCategory();
|
|
|
|
$this->secondCategory->setName("Home");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAManyToManyAssociationWithCascadeSaveSet()
|
|
|
|
{
|
|
|
|
$this->firstProduct->addCategory($this->firstCategory);
|
|
|
|
$this->firstProduct->addCategory($this->secondCategory);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstProduct);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->assertForeignKeysContain($this->firstProduct->getId(), $this->firstCategory->getId());
|
|
|
|
$this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId());
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesAManyToManyAssociation()
|
|
|
|
{
|
|
|
|
$this->firstProduct->addCategory($this->firstCategory);
|
|
|
|
$this->firstProduct->addCategory($this->secondCategory);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstProduct);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->firstProduct->removeCategory($this->firstCategory);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-11-19 16:12:00 +03:00
|
|
|
$this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->firstCategory->getId());
|
|
|
|
$this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId());
|
|
|
|
|
|
|
|
$this->firstProduct->getCategories()->remove(1);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->secondCategory->getId());
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
public function testEagerLoadFromInverseSideAndLazyLoadFromOwningSide()
|
2009-07-02 13:37:59 +04:00
|
|
|
{
|
2010-04-01 00:47:35 +04:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->_createLoadingFixture();
|
2010-02-27 20:48:18 +03:00
|
|
|
$categories = $this->_findCategories();
|
|
|
|
$this->assertLazyLoadFromOwningSide($categories);
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
public function testEagerLoadFromOwningSideAndLazyLoadFromInverseSide()
|
2009-07-02 13:37:59 +04:00
|
|
|
{
|
2010-04-01 00:47:35 +04:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->_createLoadingFixture();
|
2009-07-28 15:43:42 +04:00
|
|
|
$products = $this->_findProducts();
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->assertLazyLoadFromInverseSide($products);
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
private function _createLoadingFixture()
|
2009-07-02 13:37:59 +04:00
|
|
|
{
|
|
|
|
$this->firstProduct->addCategory($this->firstCategory);
|
|
|
|
$this->firstProduct->addCategory($this->secondCategory);
|
|
|
|
$this->secondProduct->addCategory($this->firstCategory);
|
|
|
|
$this->secondProduct->addCategory($this->secondCategory);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstProduct);
|
|
|
|
$this->_em->persist($this->secondProduct);
|
2009-07-02 13:37:59 +04:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _findProducts()
|
|
|
|
{
|
|
|
|
$query = $this->_em->createQuery('SELECT p, c FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.categories c ORDER BY p.id, c.id');
|
2009-10-15 18:39:43 +04:00
|
|
|
//$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
|
2010-02-27 20:48:18 +03:00
|
|
|
$result = $query->getResult();
|
|
|
|
$this->assertEquals(2, count($result));
|
|
|
|
$cats1 = $result[0]->getCategories();
|
|
|
|
$cats2 = $result[1]->getCategories();
|
|
|
|
$this->assertTrue($cats1->isInitialized());
|
|
|
|
$this->assertTrue($cats2->isInitialized());
|
|
|
|
$this->assertFalse($cats1[0]->getProducts()->isInitialized());
|
|
|
|
$this->assertFalse($cats2[0]->getProducts()->isInitialized());
|
|
|
|
|
|
|
|
return $result;
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
protected function _findCategories()
|
|
|
|
{
|
|
|
|
$query = $this->_em->createQuery('SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCategory c LEFT JOIN c.products p ORDER BY c.id, p.id');
|
|
|
|
//$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
|
|
|
|
$result = $query->getResult();
|
|
|
|
$this->assertEquals(2, count($result));
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $result[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $result[1]);
|
2010-02-27 20:48:18 +03:00
|
|
|
$prods1 = $result[0]->getProducts();
|
|
|
|
$prods2 = $result[1]->getProducts();
|
|
|
|
$this->assertTrue($prods1->isInitialized());
|
|
|
|
$this->assertTrue($prods2->isInitialized());
|
|
|
|
|
|
|
|
$this->assertFalse($prods1[0]->getCategories()->isInitialized());
|
|
|
|
$this->assertFalse($prods2[0]->getCategories()->isInitialized());
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function assertLazyLoadFromInverseSide($products)
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
|
|
|
list ($firstProduct, $secondProduct) = $products;
|
2009-07-28 16:30:35 +04:00
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
$firstProductCategories = $firstProduct->getCategories();
|
|
|
|
$secondProductCategories = $secondProduct->getCategories();
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($firstProductCategories));
|
|
|
|
$this->assertEquals(2, count($secondProductCategories));
|
2009-11-08 14:07:49 +03:00
|
|
|
|
2009-07-28 20:36:24 +04:00
|
|
|
$this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]);
|
|
|
|
$this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]);
|
|
|
|
|
|
|
|
$firstCategoryProducts = $firstProductCategories[0]->getProducts();
|
|
|
|
$secondCategoryProducts = $firstProductCategories[1]->getProducts();
|
2010-02-27 20:48:18 +03:00
|
|
|
|
|
|
|
$this->assertFalse($firstCategoryProducts->isInitialized());
|
|
|
|
$this->assertFalse($secondCategoryProducts->isInitialized());
|
|
|
|
$this->assertEquals(0, $firstCategoryProducts->unwrap()->count());
|
|
|
|
$this->assertEquals(0, $secondCategoryProducts->unwrap()->count());
|
2009-07-28 20:36:24 +04:00
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->assertEquals(2, count($firstCategoryProducts)); // lazy-load
|
|
|
|
$this->assertTrue($firstCategoryProducts->isInitialized());
|
|
|
|
$this->assertFalse($secondCategoryProducts->isInitialized());
|
|
|
|
$this->assertEquals(2, count($secondCategoryProducts)); // lazy-load
|
|
|
|
$this->assertTrue($secondCategoryProducts->isInitialized());
|
2009-11-08 14:07:49 +03:00
|
|
|
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstCategoryProducts[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstCategoryProducts[1]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondCategoryProducts[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondCategoryProducts[1]);
|
2009-07-28 15:43:42 +04:00
|
|
|
|
|
|
|
$this->assertCollectionEquals($firstCategoryProducts, $secondCategoryProducts);
|
|
|
|
}
|
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
public function assertLazyLoadFromOwningSide($categories)
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
2010-02-27 20:48:18 +03:00
|
|
|
list ($firstCategory, $secondCategory) = $categories;
|
|
|
|
|
|
|
|
$firstCategoryProducts = $firstCategory->getProducts();
|
|
|
|
$secondCategoryProducts = $secondCategory->getProducts();
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($firstCategoryProducts));
|
|
|
|
$this->assertEquals(2, count($secondCategoryProducts));
|
|
|
|
|
|
|
|
$this->assertTrue($firstCategoryProducts[0] === $secondCategoryProducts[0]);
|
|
|
|
$this->assertTrue($firstCategoryProducts[1] === $secondCategoryProducts[1]);
|
|
|
|
|
|
|
|
$firstProductCategories = $firstCategoryProducts[0]->getCategories();
|
|
|
|
$secondProductCategories = $firstCategoryProducts[1]->getCategories();
|
|
|
|
|
|
|
|
$this->assertFalse($firstProductCategories->isInitialized());
|
|
|
|
$this->assertFalse($secondProductCategories->isInitialized());
|
|
|
|
$this->assertEquals(0, $firstProductCategories->unwrap()->count());
|
|
|
|
$this->assertEquals(0, $secondProductCategories->unwrap()->count());
|
2009-10-23 19:03:00 +04:00
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->assertEquals(2, count($firstProductCategories)); // lazy-load
|
|
|
|
$this->assertTrue($firstProductCategories->isInitialized());
|
|
|
|
$this->assertFalse($secondProductCategories->isInitialized());
|
|
|
|
$this->assertEquals(2, count($secondProductCategories)); // lazy-load
|
|
|
|
$this->assertTrue($secondProductCategories->isInitialized());
|
|
|
|
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $firstProductCategories[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $firstProductCategories[1]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $secondProductCategories[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $secondProductCategories[1]);
|
2009-10-23 19:03:00 +04:00
|
|
|
|
2010-02-27 20:48:18 +03:00
|
|
|
$this->assertCollectionEquals($firstProductCategories, $secondProductCategories);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|