2015-06-29 22:05:26 +02:00
|
|
|
<?php
|
|
|
|
|
2017-05-03 11:06:43 +02:00
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket {
|
2015-06-29 22:05:26 +02:00
|
|
|
|
2017-05-03 10:46:24 +02:00
|
|
|
use Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Employee;
|
|
|
|
use Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Engineer;
|
|
|
|
use Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person;
|
2015-06-29 22:05:26 +02:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
|
|
|
|
2017-05-03 11:06:43 +02:00
|
|
|
class Ticket4646InstanceOfMultiLevelTest extends OrmFunctionalTestCase
|
2015-06-29 22:05:26 +02:00
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-05-03 10:46:24 +02:00
|
|
|
$this->_schemaTool->createSchema([
|
|
|
|
$this->_em->getClassMetadata(Person::class),
|
|
|
|
$this->_em->getClassMetadata(Employee::class),
|
|
|
|
$this->_em->getClassMetadata(Engineer::class),
|
|
|
|
]);
|
2015-06-29 22:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInstanceOf()
|
|
|
|
{
|
|
|
|
$this->loadData();
|
|
|
|
|
|
|
|
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person p
|
|
|
|
WHERE p INSTANCE OF Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person';
|
|
|
|
$query = $this->_em->createQuery($dql);
|
|
|
|
$result = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertCount(3, $result);
|
|
|
|
|
|
|
|
foreach ($result as $r) {
|
2017-05-03 10:46:24 +02:00
|
|
|
$this->assertInstanceOf(Person::class, $r);
|
2017-05-03 11:06:43 +02:00
|
|
|
if ($r instanceof Engineer) {
|
2015-06-29 22:05:26 +02:00
|
|
|
$this->assertEquals('foobar', $r->getName());
|
|
|
|
$this->assertEquals('doctrine', $r->getSpecialization());
|
2017-05-03 10:46:24 +02:00
|
|
|
} elseif ($r instanceof Employee) {
|
2015-06-29 22:05:26 +02:00
|
|
|
$this->assertEquals('bar', $r->getName());
|
|
|
|
$this->assertEquals('qux', $r->getDepartement());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals('foo', $r->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadData()
|
|
|
|
{
|
2017-05-03 10:46:24 +02:00
|
|
|
$person = new Person();
|
2015-06-29 22:05:26 +02:00
|
|
|
$person->setName('foo');
|
|
|
|
|
2017-05-03 10:46:24 +02:00
|
|
|
$employee = new Employee();
|
2015-06-29 22:05:26 +02:00
|
|
|
$employee->setName('bar');
|
|
|
|
$employee->setDepartement('qux');
|
|
|
|
|
2017-05-03 10:46:24 +02:00
|
|
|
$engineer = new Engineer();
|
2015-06-29 22:05:26 +02:00
|
|
|
$engineer->setName('foobar');
|
|
|
|
$engineer->setDepartement('dep');
|
|
|
|
$engineer->setSpecialization('doctrine');
|
|
|
|
|
|
|
|
$this->_em->persist($person);
|
|
|
|
$this->_em->persist($employee);
|
|
|
|
$this->_em->persist($engineer);
|
|
|
|
|
|
|
|
$this->_em->flush(array($person, $employee, $engineer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest {
|
|
|
|
/**
|
|
|
|
* @Entity()
|
|
|
|
* @Table(name="instance_of_multi_level_test_person")
|
|
|
|
* @InheritanceType(value="JOINED")
|
|
|
|
* @DiscriminatorColumn(name="kind", type="string")
|
|
|
|
* @DiscriminatorMap(value={
|
|
|
|
* "person": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person",
|
|
|
|
* "employee": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Employee",
|
|
|
|
* "engineer": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Engineer",
|
|
|
|
* })
|
|
|
|
*/
|
|
|
|
class Person
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Id()
|
|
|
|
* @GeneratedValue()
|
|
|
|
* @Column(type="integer")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(type="string")
|
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity()
|
|
|
|
* @Table(name="instance_of_multi_level_employee")
|
|
|
|
*/
|
|
|
|
class Employee extends Person
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Column(type="string")
|
|
|
|
*/
|
|
|
|
private $departement;
|
|
|
|
|
|
|
|
public function getDepartement()
|
|
|
|
{
|
|
|
|
return $this->departement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDepartement($departement)
|
|
|
|
{
|
|
|
|
$this->departement = $departement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity()
|
|
|
|
* @Table(name="instance_of_multi_level_engineer")
|
|
|
|
*/
|
|
|
|
class Engineer extends Employee
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Column(type="string")
|
|
|
|
*/
|
|
|
|
private $specialization;
|
|
|
|
|
|
|
|
public function getSpecialization()
|
|
|
|
{
|
|
|
|
return $this->specialization;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSpecialization($specialization)
|
|
|
|
{
|
|
|
|
$this->specialization = $specialization;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|