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