3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
6 use Doctrine\ORM\Mapping\AssociationMapping;
7 use Doctrine\ORM\Mapping\ClassMetadata;
9 require_once __DIR__ . '/../../TestInit.php';
12 * Tests a bidirectional one-to-one association mapping (without inheritance).
14 class OneToManySelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
20 protected function setUp()
22 $this->useModelSet('ecommerce');
24 $this->parent = new ECommerceCategory();
25 $this->parent->setName('Programming languages books');
26 $this->firstChild = new ECommerceCategory();
27 $this->firstChild->setName('Java books');
28 $this->secondChild = new ECommerceCategory();
29 $this->secondChild->setName('Php books');
32 public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
33 $this->parent->addChild($this->firstChild);
34 $this->parent->addChild($this->secondChild);
35 $this->_em->persist($this->parent);
39 $this->assertForeignKeyIs($this->parent->getId(), $this->firstChild);
40 $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
43 public function testSavesAnEmptyCollection()
45 $this->_em->persist($this->parent);
48 $this->assertEquals(0, count($this->parent->getChildren()));
51 public function testDoesNotSaveAnInverseSideSet() {
52 $this->parent->brokenAddChild($this->firstChild);
53 $this->_em->persist($this->parent);
56 $this->assertForeignKeyIs(null, $this->firstChild);
59 public function testRemovesOneToManyAssociation()
61 $this->parent->addChild($this->firstChild);
62 $this->parent->addChild($this->secondChild);
63 $this->_em->persist($this->parent);
65 $this->parent->removeChild($this->firstChild);
68 $this->assertForeignKeyIs(null, $this->firstChild);
69 $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
72 public function testEagerLoadsOneToManyAssociation()
74 $this->_createFixture();
76 $query = $this->_em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
77 $result = $query->getResult();
78 $this->assertEquals(1, count($result));
80 $children = $parent->getChildren();
82 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[0]);
83 $this->assertSame($parent, $children[0]->getParent());
84 $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
85 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[1]);
86 $this->assertSame($parent, $children[1]->getParent());
87 $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
90 public function testLazyLoadsOneToManyAssociation()
92 $this->_createFixture();
93 $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCategory');
94 $metadata->associationMappings['children']['fetch'] = ClassMetadata::FETCH_LAZY;
96 $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
97 $result = $query->getResult();
99 $children = $parent->getChildren();
101 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[0]);
102 $this->assertSame($parent, $children[0]->getParent());
103 $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
104 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[1]);
105 $this->assertSame($parent, $children[1]->getParent());
106 $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
109 private function _createFixture()
111 $this->parent->addChild($this->firstChild);
112 $this->parent->addChild($this->secondChild);
113 $this->_em->persist($this->parent);
119 public function assertForeignKeyIs($value, ECommerceCategory $child) {
120 $foreignKey = $this->_em->getConnection()->executeQuery('SELECT parent_id FROM ecommerce_categories WHERE id=?', array($child->getId()))->fetchColumn();
121 $this->assertEquals($value, $foreignKey);