2009-07-02 18:36:47 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
|
2009-07-28 15:43:42 +04:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2009-07-02 18:36:47 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a bidirectional one-to-one association mapping (without inheritance).
|
|
|
|
*/
|
|
|
|
class OneToManySelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
private $parent;
|
|
|
|
private $firstChild;
|
|
|
|
private $secondChild;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->parent = new ECommerceCategory();
|
|
|
|
$this->parent->setName('Programming languages books');
|
|
|
|
$this->firstChild = new ECommerceCategory();
|
|
|
|
$this->firstChild->setName('Java books');
|
|
|
|
$this->secondChild = new ECommerceCategory();
|
|
|
|
$this->secondChild->setName('Php books');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
|
|
|
|
$this->parent->addChild($this->firstChild);
|
|
|
|
$this->parent->addChild($this->secondChild);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->parent);
|
2009-07-02 18:36:47 +04:00
|
|
|
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-07-02 18:36:47 +04:00
|
|
|
$this->assertForeignKeyIs($this->parent->getId(), $this->firstChild);
|
|
|
|
$this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAnEmptyCollection()
|
|
|
|
{
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->parent);
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->_em->flush();
|
2009-07-02 18:36:47 +04:00
|
|
|
|
|
|
|
$this->assertEquals(0, count($this->parent->getChildren()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesNotSaveAnInverseSideSet() {
|
|
|
|
$this->parent->brokenAddChild($this->firstChild);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->parent);
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->_em->flush();
|
2009-07-02 18:36:47 +04:00
|
|
|
|
|
|
|
$this->assertForeignKeyIs(null, $this->firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesOneToManyAssociation()
|
|
|
|
{
|
|
|
|
$this->parent->addChild($this->firstChild);
|
|
|
|
$this->parent->addChild($this->secondChild);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->parent);
|
2009-07-02 18:36:47 +04:00
|
|
|
|
|
|
|
$this->parent->removeChild($this->firstChild);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertForeignKeyIs(null, $this->firstChild);
|
|
|
|
$this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEagerLoadsOneToManyAssociation()
|
|
|
|
{
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_createFixture();
|
2009-07-02 18:36:47 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-02 18:36:47 +04:00
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$parent = $result[0];
|
|
|
|
$children = $parent->getChildren();
|
|
|
|
|
|
|
|
$this->assertTrue($children[0] instanceof ECommerceCategory);
|
|
|
|
$this->assertSame($parent, $children[0]->getParent());
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
|
2009-07-02 18:36:47 +04:00
|
|
|
$this->assertTrue($children[1] instanceof ECommerceCategory);
|
|
|
|
$this->assertSame($parent, $children[1]->getParent());
|
2009-07-03 21:36:41 +04:00
|
|
|
$this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
|
2009-07-02 18:36:47 +04:00
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
|
|
|
|
public function testLazyLoadsOneToManyAssociation()
|
|
|
|
{
|
|
|
|
$this->_createFixture();
|
|
|
|
$this->_em->getConfiguration()->setAllowPartialObjects(false);
|
|
|
|
$metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCategory');
|
|
|
|
$metadata->getAssociationMapping('children')->fetchMode = AssociationMapping::FETCH_LAZY;
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-28 15:43:42 +04:00
|
|
|
$parent = $result[0];
|
|
|
|
$children = $parent->getChildren();
|
|
|
|
|
|
|
|
$this->assertTrue($children[0] instanceof ECommerceCategory);
|
|
|
|
$this->assertSame($parent, $children[0]->getParent());
|
|
|
|
$this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
|
|
|
|
$this->assertTrue($children[1] instanceof ECommerceCategory);
|
|
|
|
$this->assertSame($parent, $children[1]->getParent());
|
|
|
|
$this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _createFixture()
|
|
|
|
{
|
|
|
|
$this->parent->addChild($this->firstChild);
|
|
|
|
$this->parent->addChild($this->secondChild);
|
|
|
|
$this->_em->persist($this->parent);
|
2009-07-02 18:36:47 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
2009-07-02 18:36:47 +04:00
|
|
|
|
|
|
|
public function assertForeignKeyIs($value, ECommerceCategory $child) {
|
|
|
|
$foreignKey = $this->_em->getConnection()->execute('SELECT parent_id FROM ecommerce_categories WHERE id=?', array($child->getId()))->fetchColumn();
|
|
|
|
$this->assertEquals($value, $foreignKey);
|
|
|
|
}
|
|
|
|
}
|