1
0
mirror of synced 2025-02-09 16:59:26 +03:00
Sergio Santoro e798bfe34a
[QUERY] "INSTANCE OF" now behaves correctly with subclasses
There was a bug in the "INSTANCE OF" operator as described in
https://groups.google.com/forum/#!topic/doctrine-user/B8raq8CNMgg

"INSTANCE OF" was not taking into account subclasses.
It was merely translating the class to its discriminator.
This is not correct since the class can have subtypes and those
are, indeed, still instance of the superclass.
Also, classes may not have a discriminator (e.g. abstract classes).

This commit also provides useful tests to avoid regression.
2017-04-11 17:50:01 +02:00

120 lines
3.0 KiB
PHP

<?php
namespace Doctrine\Tests\ORM\Functional {
use Doctrine\Tests\OrmFunctionalTestCase;
class InstanceOfTest extends OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfTest\Person'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfTest\Employee'),
));
}
public function testInstanceOf()
{
$this->loadData();
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Functional\InstanceOfTest\Person p
WHERE p INSTANCE OF Doctrine\Tests\ORM\Functional\InstanceOfTest\Person';
$query = $this->_em->createQuery($dql);
$result = $query->getResult();
$this->assertCount(2, $result);
foreach ($result as $r) {
$this->assertInstanceOf('Doctrine\Tests\ORM\Functional\InstanceOfTest\Person', $r);
if ($r instanceof InstanceOfTest\Employee) {
$this->assertEquals('bar', $r->getName());
} else {
$this->assertEquals('foo', $r->getName());
}
}
}
private function loadData()
{
$person = new InstanceOfTest\Person();
$person->setName('foo');
$employee = new InstanceOfTest\Employee();
$employee->setName('bar');
$employee->setDepartement('qux');
$this->_em->persist($person);
$this->_em->persist($employee);
$this->_em->flush(array($person, $employee));
}
}
}
namespace Doctrine\Tests\ORM\Functional\InstanceOfTest {
/**
* @Entity()
* @Table(name="instance_of_test_person")
* @InheritanceType(value="JOINED")
* @DiscriminatorColumn(name="kind", type="string")
* @DiscriminatorMap(value={
* "person": "Doctrine\Tests\ORM\Functional\InstanceOfTest\Person",
* "employee": "Doctrine\Tests\ORM\Functional\InstanceOfTest\Employee"
* })
*/
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_test_employee")
*/
class Employee extends Person
{
/**
* @Column(type="string")
*/
private $departement;
public function getDepartement()
{
return $this->departement;
}
public function setDepartement($departement)
{
$this->departement = $departement;
}
}
}