OneToManySelfReferentialAssociationTest.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
  4. use Doctrine\ORM\Mapping\AssociationMapping;
  5. use Doctrine\ORM\Mapping\ClassMetadata;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a bidirectional one-to-one association mapping (without inheritance).
  9. */
  10. class OneToManySelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. private $parent;
  13. private $firstChild;
  14. private $secondChild;
  15. protected function setUp()
  16. {
  17. $this->useModelSet('ecommerce');
  18. parent::setUp();
  19. $this->parent = new ECommerceCategory();
  20. $this->parent->setName('Programming languages books');
  21. $this->firstChild = new ECommerceCategory();
  22. $this->firstChild->setName('Java books');
  23. $this->secondChild = new ECommerceCategory();
  24. $this->secondChild->setName('Php books');
  25. }
  26. public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
  27. $this->parent->addChild($this->firstChild);
  28. $this->parent->addChild($this->secondChild);
  29. $this->_em->persist($this->parent);
  30. $this->_em->flush();
  31. $this->assertForeignKeyIs($this->parent->getId(), $this->firstChild);
  32. $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
  33. }
  34. public function testSavesAnEmptyCollection()
  35. {
  36. $this->_em->persist($this->parent);
  37. $this->_em->flush();
  38. $this->assertEquals(0, count($this->parent->getChildren()));
  39. }
  40. public function testDoesNotSaveAnInverseSideSet() {
  41. $this->parent->brokenAddChild($this->firstChild);
  42. $this->_em->persist($this->parent);
  43. $this->_em->flush();
  44. $this->assertForeignKeyIs(null, $this->firstChild);
  45. }
  46. public function testRemovesOneToManyAssociation()
  47. {
  48. $this->parent->addChild($this->firstChild);
  49. $this->parent->addChild($this->secondChild);
  50. $this->_em->persist($this->parent);
  51. $this->parent->removeChild($this->firstChild);
  52. $this->_em->flush();
  53. $this->assertForeignKeyIs(null, $this->firstChild);
  54. $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
  55. }
  56. public function testEagerLoadsOneToManyAssociation()
  57. {
  58. $this->_createFixture();
  59. $query = $this->_em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
  60. $result = $query->getResult();
  61. $this->assertEquals(1, count($result));
  62. $parent = $result[0];
  63. $children = $parent->getChildren();
  64. $this->assertTrue($children[0] instanceof ECommerceCategory);
  65. $this->assertSame($parent, $children[0]->getParent());
  66. $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
  67. $this->assertTrue($children[1] instanceof ECommerceCategory);
  68. $this->assertSame($parent, $children[1]->getParent());
  69. $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
  70. }
  71. public function testLazyLoadsOneToManyAssociation()
  72. {
  73. $this->_createFixture();
  74. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCategory');
  75. $metadata->associationMappings['children']['fetch'] = ClassMetadata::FETCH_LAZY;
  76. $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
  77. $result = $query->getResult();
  78. $parent = $result[0];
  79. $children = $parent->getChildren();
  80. $this->assertTrue($children[0] instanceof ECommerceCategory);
  81. $this->assertSame($parent, $children[0]->getParent());
  82. $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
  83. $this->assertTrue($children[1] instanceof ECommerceCategory);
  84. $this->assertSame($parent, $children[1]->getParent());
  85. $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
  86. }
  87. private function _createFixture()
  88. {
  89. $this->parent->addChild($this->firstChild);
  90. $this->parent->addChild($this->secondChild);
  91. $this->_em->persist($this->parent);
  92. $this->_em->flush();
  93. $this->_em->clear();
  94. }
  95. public function assertForeignKeyIs($value, ECommerceCategory $child) {
  96. $foreignKey = $this->_em->getConnection()->executeQuery('SELECT parent_id FROM ecommerce_categories WHERE id=?', array($child->getId()))->fetchColumn();
  97. $this->assertEquals($value, $foreignKey);
  98. }
  99. }