1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional { |
4
|
|
|
|
5
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
6
|
|
|
|
7
|
|
|
class InstanceOfMultiLevelTest extends OrmFunctionalTestCase |
8
|
|
|
{ |
9
|
|
|
protected function setUp() |
10
|
|
|
{ |
11
|
|
|
parent::setUp(); |
12
|
|
|
|
13
|
|
|
$this->_schemaTool->createSchema(array( |
14
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfMultiLevelTest\Person'), |
15
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfMultiLevelTest\Employee'), |
16
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\InstanceOfMultiLevelTest\Engineer'), |
17
|
|
|
)); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testInstanceOf() |
21
|
|
|
{ |
22
|
|
|
$this->loadData(); |
23
|
|
|
|
24
|
|
|
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person p |
25
|
|
|
WHERE p INSTANCE OF Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person'; |
26
|
|
|
$query = $this->_em->createQuery($dql); |
27
|
|
|
$result = $query->getResult(); |
28
|
|
|
|
29
|
|
|
$this->assertCount(3, $result); |
30
|
|
|
|
31
|
|
|
foreach ($result as $r) { |
32
|
|
|
$this->assertInstanceOf('Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person', $r); |
33
|
|
|
if ($r instanceof InstanceOfMultiLevelTest\Engineer) { |
34
|
|
|
$this->assertEquals('foobar', $r->getName()); |
35
|
|
|
$this->assertEquals('doctrine', $r->getSpecialization()); |
36
|
|
|
} elseif ($r instanceof InstanceOfMultiLevelTest\Employee) { |
37
|
|
|
$this->assertEquals('bar', $r->getName()); |
38
|
|
|
$this->assertEquals('qux', $r->getDepartement()); |
39
|
|
|
} else { |
40
|
|
|
$this->assertEquals('foo', $r->getName()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function loadData() |
46
|
|
|
{ |
47
|
|
|
$person = new InstanceOfMultiLevelTest\Person(); |
48
|
|
|
$person->setName('foo'); |
49
|
|
|
|
50
|
|
|
$employee = new InstanceOfMultiLevelTest\Employee(); |
51
|
|
|
$employee->setName('bar'); |
52
|
|
|
$employee->setDepartement('qux'); |
53
|
|
|
|
54
|
|
|
$engineer = new InstanceOfMultiLevelTest\Engineer(); |
55
|
|
|
$engineer->setName('foobar'); |
56
|
|
|
$engineer->setDepartement('dep'); |
57
|
|
|
$engineer->setSpecialization('doctrine'); |
58
|
|
|
|
59
|
|
|
$this->_em->persist($person); |
60
|
|
|
$this->_em->persist($employee); |
61
|
|
|
$this->_em->persist($engineer); |
62
|
|
|
|
63
|
|
|
$this->_em->flush(array($person, $employee, $engineer)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
namespace Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest { |
69
|
|
|
/** |
70
|
|
|
* @Entity() |
71
|
|
|
* @Table(name="instance_of_multi_level_test_person") |
72
|
|
|
* @InheritanceType(value="JOINED") |
73
|
|
|
* @DiscriminatorColumn(name="kind", type="string") |
74
|
|
|
* @DiscriminatorMap(value={ |
75
|
|
|
* "person": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person", |
76
|
|
|
* "employee": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Employee", |
77
|
|
|
* "engineer": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Engineer", |
78
|
|
|
* }) |
79
|
|
|
*/ |
80
|
|
|
class Person |
81
|
|
|
{ |
82
|
|
|
/** |
83
|
|
|
* @Id() |
84
|
|
|
* @GeneratedValue() |
85
|
|
|
* @Column(type="integer") |
86
|
|
|
*/ |
87
|
|
|
private $id; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @Column(type="string") |
91
|
|
|
*/ |
92
|
|
|
private $name; |
93
|
|
|
|
94
|
|
|
public function getId() |
95
|
|
|
{ |
96
|
|
|
return $this->id; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getName() |
100
|
|
|
{ |
101
|
|
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setName($name) |
105
|
|
|
{ |
106
|
|
|
$this->name = $name; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @Entity() |
112
|
|
|
* @Table(name="instance_of_multi_level_employee") |
113
|
|
|
*/ |
114
|
|
|
class Employee extends Person |
115
|
|
|
{ |
116
|
|
|
/** |
117
|
|
|
* @Column(type="string") |
118
|
|
|
*/ |
119
|
|
|
private $departement; |
120
|
|
|
|
121
|
|
|
public function getDepartement() |
122
|
|
|
{ |
123
|
|
|
return $this->departement; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setDepartement($departement) |
127
|
|
|
{ |
128
|
|
|
$this->departement = $departement; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @Entity() |
134
|
|
|
* @Table(name="instance_of_multi_level_engineer") |
135
|
|
|
*/ |
136
|
|
|
class Engineer extends Employee |
137
|
|
|
{ |
138
|
|
|
/** |
139
|
|
|
* @Column(type="string") |
140
|
|
|
*/ |
141
|
|
|
private $specialization; |
142
|
|
|
|
143
|
|
|
public function getSpecialization() |
144
|
|
|
{ |
145
|
|
|
return $this->specialization; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setSpecialization($specialization) |
149
|
|
|
{ |
150
|
|
|
$this->specialization = $specialization; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|