2009-04-12 23:02:12 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2009-10-15 18:39:43 +04:00
|
|
|
use Doctrine\ORM\Query;
|
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functional tests for the Single Table Inheritance mapping strategy.
|
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
*/
|
|
|
|
class SingleTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2009-05-29 01:06:24 +04:00
|
|
|
try {
|
|
|
|
$this->_schemaTool->createSchema(array(
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ParentEntity'),
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ChildEntity'),
|
2009-12-03 16:17:56 +03:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\RelatedEntity'),
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ParentRelatedEntity')
|
2009-05-29 01:06:24 +04:00
|
|
|
));
|
|
|
|
} catch (\Exception $e) {
|
2009-05-30 13:37:56 +04:00
|
|
|
// Swallow all exceptions. We do not test the schema tool here.
|
2009-05-29 01:06:24 +04:00
|
|
|
}
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
public function testCRUD()
|
2009-05-29 14:23:13 +04:00
|
|
|
{
|
2009-04-12 23:02:12 +04:00
|
|
|
$parent = new ParentEntity;
|
|
|
|
$parent->setData('foobar');
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($parent);
|
2009-05-03 14:58:16 +04:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
$child = new ChildEntity;
|
|
|
|
$child->setData('thedata');
|
|
|
|
$child->setNumber(1234);
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($child);
|
2009-05-03 15:49:48 +04:00
|
|
|
|
|
|
|
$relatedEntity = new RelatedEntity;
|
|
|
|
$relatedEntity->setName('theRelatedOne');
|
|
|
|
$relatedEntity->setOwner($child);
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($relatedEntity);
|
2009-05-29 01:06:24 +04:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\ParentEntity e order by e.data asc");
|
2009-10-15 18:39:43 +04:00
|
|
|
$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
|
2009-08-03 21:18:37 +04:00
|
|
|
$entities = $query->getResult();
|
2009-07-19 20:54:53 +04:00
|
|
|
|
2009-04-12 23:02:12 +04:00
|
|
|
$this->assertEquals(2, count($entities));
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->assertTrue(is_numeric($entities[0]->getId()));
|
|
|
|
$this->assertTrue(is_numeric($entities[1]->getId()));
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->assertTrue($entities[0] instanceof ParentEntity);
|
2009-12-03 16:17:56 +03:00
|
|
|
$this->assertTrue($entities[1] instanceof ChildEntity);
|
|
|
|
$this->assertNull($entities[0]->getParentRelated());
|
|
|
|
$this->assertNull($entities[1]->getParentRelated());
|
2009-04-12 23:02:12 +04:00
|
|
|
$this->assertEquals('foobar', $entities[0]->getData());
|
|
|
|
$this->assertEquals('thedata', $entities[1]->getData());
|
|
|
|
$this->assertEquals(1234, $entities[1]->getNumber());
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\ChildEntity e");
|
2009-08-03 21:18:37 +04:00
|
|
|
$entities = $query->getResult();
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->assertEquals(1, count($entities));
|
|
|
|
$this->assertTrue($entities[0] instanceof ChildEntity);
|
|
|
|
$this->assertTrue(is_numeric($entities[0]->getId()));
|
|
|
|
$this->assertEquals('thedata', $entities[0]->getData());
|
|
|
|
$this->assertEquals(1234, $entities[0]->getNumber());
|
2009-12-03 16:17:56 +03:00
|
|
|
$this->assertNull($entities[0]->getParentRelated());
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select r,o from Doctrine\Tests\ORM\Functional\RelatedEntity r join r.owner o");
|
|
|
|
|
2009-08-03 21:18:37 +04:00
|
|
|
$entities = $query->getResult();
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->assertEquals(1, count($entities));
|
|
|
|
$this->assertTrue($entities[0] instanceof RelatedEntity);
|
|
|
|
$this->assertTrue(is_numeric($entities[0]->getId()));
|
|
|
|
$this->assertEquals('theRelatedOne', $entities[0]->getName());
|
|
|
|
$this->assertTrue($entities[0]->getOwner() instanceof ChildEntity);
|
|
|
|
$this->assertEquals('thedata', $entities[0]->getOwner()->getData());
|
|
|
|
$this->assertSame($entities[0], $entities[0]->getOwner()->getRelatedEntity());
|
2009-12-03 16:17:56 +03:00
|
|
|
$this->assertNull($entities[0]->getOwner()->getParentRelated());
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery("update Doctrine\Tests\ORM\Functional\ChildEntity e set e.data = 'newdata'");
|
|
|
|
|
|
|
|
$affected = $query->execute();
|
|
|
|
$this->assertEquals(1, $affected);
|
2009-08-13 14:13:06 +04:00
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
$query = $this->_em->createQuery("delete Doctrine\Tests\ORM\Functional\ParentEntity e");
|
|
|
|
|
|
|
|
$affected = $query->execute();
|
2009-11-13 13:11:14 +03:00
|
|
|
$this->assertEquals(2, $affected);
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
// DQL query with WHERE clause
|
|
|
|
$child = new ChildEntity;
|
|
|
|
$child->setData('thedata');
|
|
|
|
$child->setNumber(1234);
|
|
|
|
|
|
|
|
$this->_em->persist($child);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select e from Doctrine\Tests\ORM\Functional\ParentEntity e where e.id=?1');
|
|
|
|
$query->setParameter(1, $child->getId());
|
|
|
|
|
|
|
|
$child2 = $query->getSingleResult();
|
|
|
|
$this->assertTrue($child2 instanceof ChildEntity);
|
|
|
|
$this->assertEquals('thedata', $child2->getData());
|
|
|
|
$this->assertEquals(1234, $child2->getNumber());
|
|
|
|
$this->assertEquals($child->getId(), $child2->getId());
|
|
|
|
$this->assertFalse($child === $child2);
|
2009-12-03 16:17:56 +03:00
|
|
|
$this->assertNull($child2->getParentRelated());
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2009-11-16 20:03:33 +03:00
|
|
|
|
|
|
|
public function testGetScalarResult()
|
|
|
|
{
|
|
|
|
$child = new ChildEntity;
|
|
|
|
$child->setData('thedata');
|
|
|
|
$child->setNumber(1234);
|
|
|
|
|
|
|
|
$this->_em->persist($child);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select e from Doctrine\Tests\ORM\Functional\ParentEntity e where e.id=?1');
|
|
|
|
$query->setParameter(1, $child->getId());
|
|
|
|
|
|
|
|
$result = $query->getScalarResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$this->assertEquals($child->getId(), $result[0]['e_id']);
|
|
|
|
$this->assertEquals('thedata', $result[0]['e_data']);
|
|
|
|
$this->assertEquals(1234, $result[0]['e_number']);
|
|
|
|
$this->assertNull($result[0]['e_related_entity_id']);
|
|
|
|
$this->assertEquals('child', $result[0]['e_discr']);
|
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
|
2009-12-03 16:17:56 +03:00
|
|
|
public function testPolymorphicFindAndQuery()
|
2009-11-21 21:52:02 +03:00
|
|
|
{
|
|
|
|
$child = new ChildEntity;
|
|
|
|
$child->setData('thedata');
|
|
|
|
$child->setNumber(1234);
|
|
|
|
|
|
|
|
$this->_em->persist($child);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$child2 = $this->_em->find('Doctrine\Tests\ORM\Functional\ParentEntity', $child->getId());
|
|
|
|
|
|
|
|
$this->assertTrue($child2 instanceof ChildEntity);
|
|
|
|
$this->assertEquals('thedata', $child2->getData());
|
|
|
|
$this->assertSame(1234, $child2->getNumber());
|
2009-12-03 16:17:56 +03:00
|
|
|
|
|
|
|
$parentRelated = new ParentRelatedEntity;
|
|
|
|
$parentRelated->setData('related to parent!');
|
|
|
|
$child2->setParentRelated($parentRelated);
|
|
|
|
$parentRelated->setParent($child2);
|
|
|
|
$this->_em->persist($parentRelated);
|
|
|
|
|
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSqlLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select p, r from Doctrine\Tests\ORM\Functional\ParentEntity p join p.parentRelated r");
|
|
|
|
$result = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$this->assertTrue($result[0] instanceof ChildEntity);
|
|
|
|
$related = $result[0]->getParentRelated();
|
|
|
|
$this->assertFalse($related instanceof \Doctrine\ORM\Proxy\Proxy);
|
|
|
|
$this->assertTrue($related instanceof ParentRelatedEntity);
|
|
|
|
$this->assertEquals('related to parent!', $related->getData());
|
2009-11-21 21:52:02 +03:00
|
|
|
}
|
2009-12-03 16:17:56 +03:00
|
|
|
|
|
|
|
/*public function testPolymorphicQueryWithJoin()
|
|
|
|
{
|
|
|
|
|
|
|
|
}*/
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Entity
|
2009-06-07 21:20:37 +04:00
|
|
|
* @InheritanceType("SINGLE_TABLE")
|
2009-05-29 14:23:13 +04:00
|
|
|
* @DiscriminatorColumn(name="discr", type="string")
|
2009-08-17 21:58:16 +04:00
|
|
|
* @DiscriminatorMap({"parent"="ParentEntity", "child"="ChildEntity"})
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
|
|
|
class ParentEntity {
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Id
|
|
|
|
* @Column(type="integer")
|
2009-06-07 21:20:37 +04:00
|
|
|
* @GeneratedValue(strategy="AUTO")
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
2009-08-15 22:11:51 +04:00
|
|
|
* @Column(name="DATA", type="string")
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
|
|
|
private $data;
|
2009-12-03 16:17:56 +03:00
|
|
|
|
|
|
|
/** @OneToOne(targetEntity="ParentRelatedEntity", mappedBy="parent") */
|
|
|
|
private $parentRelated;
|
2009-04-12 23:02:12 +04:00
|
|
|
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getData() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setData($data) {
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
2009-12-03 16:17:56 +03:00
|
|
|
|
|
|
|
public function getParentRelated() {
|
|
|
|
return $this->parentRelated;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setParentRelated($parentRelated) {
|
|
|
|
$this->parentRelated = $parentRelated;
|
|
|
|
}
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Entity
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
|
|
|
class ChildEntity extends ParentEntity {
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Column(name="`number`", type="integer", nullable=true)
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
|
|
|
private $number;
|
2009-05-03 14:58:16 +04:00
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @OneToOne(targetEntity="RelatedEntity")
|
|
|
|
* @JoinColumn(name="related_entity_id", referencedColumnName="id")
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
|
|
|
private $relatedEntity;
|
2009-04-12 23:02:12 +04:00
|
|
|
|
|
|
|
public function getNumber() {
|
|
|
|
return $this->number;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setNumber($number) {
|
|
|
|
$this->number = $number;
|
|
|
|
}
|
2009-05-03 14:58:16 +04:00
|
|
|
|
|
|
|
public function getRelatedEntity() {
|
|
|
|
return $this->relatedEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRelatedEntity($relatedEntity) {
|
|
|
|
$this->relatedEntity = $relatedEntity;
|
|
|
|
$relatedEntity->setOwner($this);
|
|
|
|
}
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Entity
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
|
|
|
class RelatedEntity {
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Id
|
|
|
|
* @Column(type="integer")
|
2009-06-07 21:20:37 +04:00
|
|
|
* @GeneratedValue(strategy="AUTO")
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @Column(type="string", length=50)
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
/**
|
2009-05-29 14:23:13 +04:00
|
|
|
* @OneToOne(targetEntity="ChildEntity", mappedBy="relatedEntity")
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
|
|
|
private $owner;
|
|
|
|
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName($name) {
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOwner() {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOwner($owner) {
|
|
|
|
$this->owner = $owner;
|
|
|
|
if ($owner->getRelatedEntity() !== $this) {
|
|
|
|
$owner->setRelatedEntity($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-03 16:17:56 +03:00
|
|
|
|
|
|
|
/** @Entity */
|
|
|
|
class ParentRelatedEntity {
|
|
|
|
/**
|
|
|
|
* @Id @Column(type="integer")
|
|
|
|
* @GeneratedValue(strategy="AUTO")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
public function getId() {return $this->id;}
|
|
|
|
/** @Column(type="string") */
|
|
|
|
private $data;
|
|
|
|
public function getData() {return $this->data;}
|
|
|
|
public function setData($data) {$this->data = $data;}
|
|
|
|
/**
|
|
|
|
* @OneToOne(targetEntity="ParentEntity")
|
|
|
|
* @JoinColumn(name="parent_id", referencedColumnName="id")
|
|
|
|
*/
|
|
|
|
private $parent;
|
|
|
|
public function getParent() {return $this->parent;}
|
|
|
|
public function setParent($parent) {$this->parent = $parent;}
|
|
|
|
}
|