2009-07-02 13:37:59 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
|
|
|
|
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-02 13:37:59 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a unidirectional many-to-many association mapping (without inheritance).
|
|
|
|
* Inverse side is not present.
|
|
|
|
*/
|
|
|
|
class ManyToManyUnidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
|
|
|
|
{
|
|
|
|
protected $_firstField = 'cart_id';
|
|
|
|
protected $_secondField = 'product_id';
|
|
|
|
protected $_table = 'ecommerce_carts_products';
|
|
|
|
private $firstProduct;
|
|
|
|
private $secondProduct;
|
|
|
|
private $firstCart;
|
|
|
|
private $secondCart;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->firstProduct = new ECommerceProduct();
|
|
|
|
$this->firstProduct->setName('Doctrine 1.x Manual');
|
|
|
|
$this->secondProduct = new ECommerceProduct();
|
|
|
|
$this->secondProduct->setName('Doctrine 2.x Manual');
|
|
|
|
$this->firstCart = new ECommerceCart();
|
|
|
|
$this->secondCart = new ECommerceCart();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAManyToManyAssociationWithCascadeSaveSet()
|
|
|
|
{
|
|
|
|
$this->firstCart->addProduct($this->firstProduct);
|
|
|
|
$this->firstCart->addProduct($this->secondProduct);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstCart);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-07-02 15:48:44 +04:00
|
|
|
$this->assertForeignKeysContain($this->firstCart->getId(), $this->firstProduct->getId());
|
|
|
|
$this->assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesAManyToManyAssociation()
|
|
|
|
{
|
|
|
|
$this->firstCart->addProduct($this->firstProduct);
|
|
|
|
$this->firstCart->addProduct($this->secondProduct);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->firstCart);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->firstCart->removeProduct($this->firstProduct);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-07-02 15:48:44 +04:00
|
|
|
$this->assertForeignKeysNotContain($this->firstCart->getId(), $this->firstProduct->getId());
|
|
|
|
$this->assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEagerLoad()
|
|
|
|
{
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_createFixture();
|
2009-07-02 13:37:59 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c LEFT JOIN c.products p ORDER BY c.id, p.id');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-02 13:37:59 +04:00
|
|
|
$firstCart = $result[0];
|
|
|
|
$products = $firstCart->getProducts();
|
|
|
|
$secondCart = $result[1];
|
|
|
|
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[1]);
|
2009-07-02 13:37:59 +04:00
|
|
|
$this->assertCollectionEquals($products, $secondCart->getProducts());
|
|
|
|
//$this->assertEquals("Doctrine 1.x Manual", $products[0]->getName());
|
|
|
|
//$this->assertEquals("Doctrine 2.x Manual", $products[1]->getName());
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
public function testLazyLoadsCollection()
|
|
|
|
{
|
|
|
|
$this->_createFixture();
|
|
|
|
$metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
|
2010-08-09 15:13:21 +04:00
|
|
|
$metadata->associationMappings['products']['fetch'] = ClassMetadata::FETCH_LAZY;
|
2009-07-28 15:43:42 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('SELECT c FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-28 15:43:42 +04:00
|
|
|
$firstCart = $result[0];
|
|
|
|
$products = $firstCart->getProducts();
|
|
|
|
$secondCart = $result[1];
|
2009-07-02 13:37:59 +04:00
|
|
|
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[0]);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[1]);
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->assertCollectionEquals($products, $secondCart->getProducts());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _createFixture()
|
|
|
|
{
|
|
|
|
$this->firstCart->addProduct($this->firstProduct);
|
|
|
|
$this->firstCart->addProduct($this->secondProduct);
|
|
|
|
$this->secondCart->addProduct($this->firstProduct);
|
|
|
|
$this->secondCart->addProduct($this->secondProduct);
|
|
|
|
$this->_em->persist($this->firstCart);
|
|
|
|
$this->_em->persist($this->secondCart);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
2009-07-02 13:37:59 +04:00
|
|
|
}
|