ManyToManyUnidirectionalAssociationTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceCart;
  4. use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
  5. use Doctrine\ORM\Mapping\AssociationMapping;
  6. use Doctrine\ORM\Mapping\ClassMetadata;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. /**
  9. * Tests a unidirectional many-to-many association mapping (without inheritance).
  10. * Inverse side is not present.
  11. */
  12. class ManyToManyUnidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
  13. {
  14. protected $_firstField = 'cart_id';
  15. protected $_secondField = 'product_id';
  16. protected $_table = 'ecommerce_carts_products';
  17. private $firstProduct;
  18. private $secondProduct;
  19. private $firstCart;
  20. private $secondCart;
  21. protected function setUp()
  22. {
  23. $this->useModelSet('ecommerce');
  24. parent::setUp();
  25. $this->firstProduct = new ECommerceProduct();
  26. $this->firstProduct->setName('Doctrine 1.x Manual');
  27. $this->secondProduct = new ECommerceProduct();
  28. $this->secondProduct->setName('Doctrine 2.x Manual');
  29. $this->firstCart = new ECommerceCart();
  30. $this->secondCart = new ECommerceCart();
  31. }
  32. public function testSavesAManyToManyAssociationWithCascadeSaveSet()
  33. {
  34. $this->firstCart->addProduct($this->firstProduct);
  35. $this->firstCart->addProduct($this->secondProduct);
  36. $this->_em->persist($this->firstCart);
  37. $this->_em->flush();
  38. $this->assertForeignKeysContain($this->firstCart->getId(), $this->firstProduct->getId());
  39. $this->assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
  40. }
  41. public function testRemovesAManyToManyAssociation()
  42. {
  43. $this->firstCart->addProduct($this->firstProduct);
  44. $this->firstCart->addProduct($this->secondProduct);
  45. $this->_em->persist($this->firstCart);
  46. $this->firstCart->removeProduct($this->firstProduct);
  47. $this->_em->flush();
  48. $this->assertForeignKeysNotContain($this->firstCart->getId(), $this->firstProduct->getId());
  49. $this->assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
  50. }
  51. public function testEagerLoad()
  52. {
  53. $this->_createFixture();
  54. $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');
  55. $result = $query->getResult();
  56. $firstCart = $result[0];
  57. $products = $firstCart->getProducts();
  58. $secondCart = $result[1];
  59. $this->assertTrue($products[0] instanceof ECommerceProduct);
  60. $this->assertTrue($products[1] instanceof ECommerceProduct);
  61. $this->assertCollectionEquals($products, $secondCart->getProducts());
  62. //$this->assertEquals("Doctrine 1.x Manual", $products[0]->getName());
  63. //$this->assertEquals("Doctrine 2.x Manual", $products[1]->getName());
  64. }
  65. public function testLazyLoadsCollection()
  66. {
  67. $this->_createFixture();
  68. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
  69. $metadata->associationMappings['products']['fetch'] = ClassMetadata::FETCH_LAZY;
  70. $query = $this->_em->createQuery('SELECT c FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c');
  71. $result = $query->getResult();
  72. $firstCart = $result[0];
  73. $products = $firstCart->getProducts();
  74. $secondCart = $result[1];
  75. $this->assertTrue($products[0] instanceof ECommerceProduct);
  76. $this->assertTrue($products[1] instanceof ECommerceProduct);
  77. $this->assertCollectionEquals($products, $secondCart->getProducts());
  78. }
  79. private function _createFixture()
  80. {
  81. $this->firstCart->addProduct($this->firstProduct);
  82. $this->firstCart->addProduct($this->secondProduct);
  83. $this->secondCart->addProduct($this->firstProduct);
  84. $this->secondCart->addProduct($this->secondProduct);
  85. $this->_em->persist($this->firstCart);
  86. $this->_em->persist($this->secondCart);
  87. $this->_em->flush();
  88. $this->_em->clear();
  89. }
  90. }