2009-04-12 23:02:12 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
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();
|
|
|
|
$this->_schemaTool->createSchema(array(
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ParentEntity'),
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ChildEntity'),
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\RelatedEntity')
|
2009-04-12 23:02:12 +04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2009-05-03 14:58:16 +04:00
|
|
|
public function testCRUD()
|
2009-04-12 23:02:12 +04:00
|
|
|
{
|
|
|
|
$parent = new ParentEntity;
|
|
|
|
$parent->setData('foobar');
|
|
|
|
|
|
|
|
$this->_em->save($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);
|
|
|
|
|
|
|
|
$this->_em->save($child);
|
2009-05-03 15:49:48 +04:00
|
|
|
|
|
|
|
$relatedEntity = new RelatedEntity;
|
|
|
|
$relatedEntity->setName('theRelatedOne');
|
|
|
|
$relatedEntity->setOwner($child);
|
|
|
|
|
|
|
|
$this->_em->save($relatedEntity);
|
2009-04-12 23:02:12 +04:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
$query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\ParentEntity e order by e.id asc");
|
2009-04-12 23:02:12 +04:00
|
|
|
|
|
|
|
$entities = $query->getResultList();
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($entities));
|
|
|
|
$this->assertTrue($entities[0] instanceof ParentEntity);
|
|
|
|
$this->assertTrue($entities[1] instanceof ChildEntity);
|
2009-05-03 14:58:16 +04:00
|
|
|
$this->assertTrue(is_numeric($entities[0]->getId()));
|
|
|
|
$this->assertTrue(is_numeric($entities[1]->getId()));
|
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");
|
|
|
|
|
|
|
|
$entities = $query->getResultList();
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("select r,o from Doctrine\Tests\ORM\Functional\RelatedEntity r join r.owner o");
|
|
|
|
|
|
|
|
$entities = $query->getResultList();
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("update Doctrine\Tests\ORM\Functional\ChildEntity e set e.data = 'newdata'");
|
|
|
|
|
|
|
|
$affected = $query->execute();
|
|
|
|
$this->assertEquals(1, $affected);
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery("delete Doctrine\Tests\ORM\Functional\ParentEntity e");
|
|
|
|
|
|
|
|
$affected = $query->execute();
|
|
|
|
$this->assertEquals(2, $affected);
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @DoctrineEntity
|
|
|
|
* @DoctrineInheritanceType("singleTable")
|
2009-05-13 19:19:27 +04:00
|
|
|
* @DoctrineDiscriminatorColumn(name="discr", type="string")
|
2009-04-12 23:02:12 +04:00
|
|
|
* @DoctrineSubClasses({"Doctrine\Tests\ORM\Functional\ChildEntity"})
|
|
|
|
* @DoctrineDiscriminatorValue("parent")
|
|
|
|
*/
|
|
|
|
class ParentEntity {
|
|
|
|
/**
|
|
|
|
* @DoctrineId
|
|
|
|
* @DoctrineColumn(type="integer")
|
|
|
|
* @DoctrineGeneratedValue(strategy="auto")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
2009-05-13 19:19:27 +04:00
|
|
|
* @DoctrineColumn(type="string")
|
2009-04-12 23:02:12 +04:00
|
|
|
*/
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getData() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setData($data) {
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @DoctrineEntity
|
|
|
|
* @DoctrineDiscriminatorValue("child")
|
|
|
|
*/
|
|
|
|
class ChildEntity extends ParentEntity {
|
|
|
|
/**
|
|
|
|
* @DoctrineColumn(type="integer", nullable=true)
|
|
|
|
*/
|
|
|
|
private $number;
|
2009-05-03 14:58:16 +04:00
|
|
|
/**
|
|
|
|
* @DoctrineOneToOne(targetEntity="RelatedEntity")
|
|
|
|
* @DoctrineJoinColumn(name="related_entity_id", referencedColumnName="id")
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @DoctrineEntity
|
|
|
|
*/
|
|
|
|
class RelatedEntity {
|
|
|
|
/**
|
|
|
|
* @DoctrineId
|
|
|
|
* @DoctrineColumn(type="integer")
|
|
|
|
* @DoctrineGeneratedValue(strategy="auto")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
/**
|
2009-05-13 19:19:27 +04:00
|
|
|
* @DoctrineColumn(type="string", length=50)
|
2009-05-03 14:58:16 +04:00
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
/**
|
|
|
|
* @DoctrineOneToOne(targetEntity="ChildEntity", mappedBy="relatedEntity")
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|