-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct DQL INSTANCE OF
to filter all possible child classes
#6392
Changes from 1 commit
e798bfe
4eb4465
3219fe5
11c84c7
21e12ef
96bcee4
30256e7
11c54b7
8b9c297
ba69cc8
d4cdc6e
7d98135
04acde6
aa233f8
0e88f1b
bd47ec9
31d2d84
5181eae
167dfde
d2f7514
2fd8752
b1f7c59
e91dcf8
c7ef908
d4db126
5224a89
9864a5a
19bc499
c799c6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional { | ||
|
||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class InstanceOfAbstractTest extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_schemaTool->createSchema(array( | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfAbstractTest\Person'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfAbstractTest\Employee'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
)); | ||
} | ||
|
||
public function testInstanceOf() | ||
{ | ||
$this->loadData(); | ||
|
||
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Functional\InstanceOfAbstractTest\Person p | ||
WHERE p INSTANCE OF Doctrine\Tests\ORM\Functional\InstanceOfAbstractTest\Person'; | ||
$query = $this->_em->createQuery($dql); | ||
$result = $query->getResult(); | ||
|
||
$this->assertCount(1, $result); | ||
|
||
foreach ($result as $r) { | ||
$this->assertInstanceOf('Doctrine\Tests\ORM\Functional\InstanceOfAbstractTest\Person', $r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, use |
||
$this->assertInstanceOf('Doctrine\Tests\ORM\Functional\InstanceOfAbstractTest\Employee', $r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
$this->assertEquals('bar', $r->getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, use |
||
} | ||
} | ||
|
||
private function loadData() | ||
{ | ||
$employee = new InstanceOfAbstractTest\Employee(); | ||
$employee->setName('bar'); | ||
$employee->setDepartement('qux'); | ||
|
||
$this->_em->persist($employee); | ||
|
||
$this->_em->flush($employee); | ||
} | ||
} | ||
} | ||
|
||
namespace Doctrine\Tests\ORM\Functional\InstanceOfAbstractTest { | ||
|
||
/** | ||
* @Entity() | ||
* @Table(name="instance_of_abstract_test_person") | ||
* @InheritanceType(value="JOINED") | ||
* @DiscriminatorColumn(name="kind", type="string") | ||
* @DiscriminatorMap(value={ | ||
* "employee": "Doctrine\Tests\ORM\Functional\InstanceOfAbstractTest\Employee" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
* }) | ||
*/ | ||
abstract 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_abstract_test_employee") | ||
*/ | ||
class Employee extends Person | ||
{ | ||
/** | ||
* @Column(type="string") | ||
*/ | ||
private $departement; | ||
|
||
public function getDepartement() | ||
{ | ||
return $this->departement; | ||
} | ||
|
||
public function setDepartement($departement) | ||
{ | ||
$this->departement = $departement; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional { | ||
|
||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class InstanceOfMultiLevelTest extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->_schemaTool->createSchema(array( | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfMultiLevelTest\Person'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfMultiLevelTest\Employee'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfMultiLevelTest\Engineer'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
)); | ||
} | ||
|
||
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) { | ||
$this->assertInstanceOf('Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person', $r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
if ($r instanceof InstanceOfMultiLevelTest\Engineer) { | ||
$this->assertEquals('foobar', $r->getName()); | ||
$this->assertEquals('doctrine', $r->getSpecialization()); | ||
} elseif ($r instanceof InstanceOfMultiLevelTest\Employee) { | ||
$this->assertEquals('bar', $r->getName()); | ||
$this->assertEquals('qux', $r->getDepartement()); | ||
} else { | ||
$this->assertEquals('foo', $r->getName()); | ||
} | ||
} | ||
} | ||
|
||
private function loadData() | ||
{ | ||
$person = new InstanceOfMultiLevelTest\Person(); | ||
$person->setName('foo'); | ||
|
||
$employee = new InstanceOfMultiLevelTest\Employee(); | ||
$employee->setName('bar'); | ||
$employee->setDepartement('qux'); | ||
|
||
$engineer = new InstanceOfMultiLevelTest\Engineer(); | ||
$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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use short array syntax