Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / Ticket69.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\ORM\Query;
6
7 require_once __DIR__ . '/../../../TestInit.php';
8
9 /**
10  * Functional tests for the Single Table Inheritance mapping strategy.
11  *
12  * @author robo
13  */
14 class AdvancedAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase {
15     protected function setUp()
16     {
17         parent::setUp();
18         try {
19             $this->_schemaTool->createSchema(array(
20                     $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\Lemma'),
21                     $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\Relation'),
22                     $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\RelationType')
23             ));
24         } catch (\Exception $e) {
25             // Swallow all exceptions. We do not test the schema tool here.
26         }
27     }
28
29     public function testIssue()
30     {
31         //setup
32         $lemma1 = new Lemma;
33         $lemma1->setLemma('foo');
34
35         $lemma2 = new Lemma;
36         $lemma2->setLemma('bar');
37
38         $lemma3 = new Lemma;
39         $lemma3->setLemma('batz');
40
41         $lemma4 = new Lemma;
42         $lemma4->setLemma('bla');
43
44         $type1 = new RelationType;
45         $type1->setType('nonsense');
46         $type1->setAbbreviation('non');
47
48         $type2 = new RelationType;
49         $type2->setType('quatsch');
50         $type2->setAbbreviation('qu');
51
52         $relation1 = new Relation;
53         $relation1->setParent($lemma1);
54         $relation1->setChild($lemma2);
55         $relation1->setType($type1);
56
57         $relation2 = new Relation;
58         $relation2->setParent($lemma1);
59         $relation2->setChild($lemma3);
60         $relation2->setType($type1);
61
62         $relation3 = new Relation;
63         $relation3->setParent($lemma1);
64         $relation3->setChild($lemma4);
65         $relation3->setType($type2);
66
67         $lemma1->addRelation($relation1);
68         $lemma1->addRelation($relation2);
69         $lemma1->addRelation($relation3);
70
71         $this->_em->persist($type1);
72         $this->_em->persist($type2);
73         $this->_em->persist($lemma1);
74         $this->_em->persist($lemma2);
75         $this->_em->persist($lemma3);
76         $this->_em->persist($lemma4);
77
78         $this->_em->flush();
79         $this->_em->clear();
80         //end setup
81
82         // test One To Many
83         $query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Ticket\Lemma l Where l.lemma = 'foo'");
84         $res = $query->getResult();
85         $lemma = $res[0];
86
87         $this->assertEquals('foo', $lemma->getLemma());
88         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\Lemma', $lemma);
89         $relations = $lemma->getRelations();
90
91         foreach($relations as $relation) {
92             $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\Relation', $relation);
93             $this->assertTrue($relation->getType()->getType() != '');
94         }
95
96         $this->_em->clear();
97
98     }
99 }
100
101 /**
102  * @Entity
103  * @Table(name="lemma")
104  */
105 class Lemma {
106
107     const CLASS_NAME = __CLASS__;
108
109     /**
110      * @var int
111      * @Id
112      * @Column(type="integer", name="lemma_id")
113      * @GeneratedValue(strategy="AUTO")
114      */
115     private $id;
116
117     /**
118      *
119      * @var string
120      * @Column(type="string", name="lemma_name", unique=true, length=255)
121      */
122     private $lemma;
123
124
125     /**
126      * @var kateglo\application\utilities\collections\ArrayCollection
127      * @OneToMany(targetEntity="Relation", mappedBy="parent", cascade={"persist"})
128      */
129     private $relations;
130
131     public function __construct() {
132         $this->types = new \Doctrine\Common\Collections\ArrayCollection();
133         $this->relations = new \Doctrine\Common\Collections\ArrayCollection();
134     }
135
136
137     /**
138      *
139      * @return int
140      */
141     public function getId() {
142         return $this->id;
143     }
144
145     /**
146      *
147      * @param string $lemma
148      * @return void
149      */
150     public function setLemma($lemma) {
151         $this->lemma = $lemma;
152     }
153
154     /**
155      *
156      * @return string
157      */
158     public function getLemma() {
159         return $this->lemma;
160     }
161
162
163     /**
164      *
165      * @param Relation $relation
166      * @return void
167      */
168     public function addRelation(Relation $relation) {
169         $this->relations[] = $relation;
170         $relation->setParent($this);
171     }
172
173     /**
174      *
175      * @param Relation $relation
176      * @return void
177      */
178     public function removeRelation(Relation $relation) {
179         /*@var $removed Relation */
180         $removed = $this->relations->removeElement($relation);
181         if ($removed !== null) {
182             $removed->removeParent();
183         }
184     }
185
186     /**
187      *
188      * @return kateglo\application\utilities\collections\ArrayCollection
189      */
190     public function getRelations() {
191         return $this->relations;
192     }
193
194 }
195
196 /**
197  *
198  * @Entity
199  * @Table(name="relation")
200  */
201 class Relation {
202
203     const CLASS_NAME = __CLASS__;
204
205     /**
206      * @var int
207      * @Id
208      * @Column(type="integer", name="relation_id")
209      * @GeneratedValue(strategy="AUTO")
210      */
211     private $id;
212
213     /**
214      * @var Lemma
215      * @ManyToOne(targetEntity="Lemma", inversedBy="relations")
216      * @JoinColumn(name="relation_parent_id", referencedColumnName="lemma_id")
217      */
218     private $parent;
219
220     /**
221      * @var Lemma
222      * @OneToOne(targetEntity="Lemma")
223      * @JoinColumn(name="relation_child_id", referencedColumnName="lemma_id")
224      */
225     private $child;
226
227     /**
228      * @var RelationType
229      * @ManyToOne(targetEntity="RelationType", inversedBy="relations")
230      * @JoinColumn(name="relation_type_id", referencedColumnName="relation_type_id")
231      */
232     private $type;
233
234     /**
235      *
236      * @param Lemma $parent
237      * @return void
238      */
239     public function setParent(Lemma $parent) {
240         $this->parent = $parent;
241     }
242
243     /**
244      *
245      * @return Phrase
246      */
247     public function getParent() {
248         return $this->parent;
249     }
250
251     /**
252      *
253      * @return void
254      */
255     public function removeParent() {
256         if ($this->lemma !== null) {
257             /*@var $phrase Lemma */
258             $lemma = $this->parent;
259             $this->parent = null;
260             $lemma->removeRelation($this);
261         }
262     }
263
264     /**
265      *
266      * @param Lemma $child
267      * @return void
268      */
269     public function setChild(Lemma $child) {
270         $this->child = $child;
271     }
272
273     /**
274      *
275      * @return Lemma
276      */
277     public function getChild() {
278         return $this->child;
279     }
280
281     /**
282      *
283      * @param RelationType $type
284      * @return void
285      */
286     public function setType(RelationType $type) {
287         $this->type = $type;
288     }
289
290     /**
291      *
292      * @return RelationType
293      */
294     public function getType() {
295         return $this->type;
296     }
297
298     /**
299      *
300      * @return void
301      */
302     public function removeType() {
303         if ($this->type !== null) {
304             /*@var $phrase RelationType */
305             $type = $this->type;
306             $this->type = null;
307             $type->removeRelation($this);
308         }
309     }
310 }
311
312 /**
313  *
314  * @Entity
315  * @Table(name="relation_type")
316  */
317 class RelationType {
318
319     const CLASS_NAME = __CLASS__;
320
321     /**
322      *
323      * @var int
324      * @Id
325      * @Column(type="integer", name="relation_type_id")
326      * @GeneratedValue(strategy="AUTO")
327      */
328     private $id;
329
330     /**
331      *
332      * @var string
333      * @Column(type="string", name="relation_type_name", unique=true, length=255)
334      */
335     private $type;
336
337     /**
338      *
339      * @var string
340      * @Column(type="string", name="relation_type_abbreviation", unique=true, length=255)
341      */
342     private $abbreviation;
343
344     /**
345      * @var kateglo\application\utilities\collections\ArrayCollection
346      * @OneToMany(targetEntity="Relation", mappedBy="type", cascade={"persist"})
347      */
348     private $relations;
349
350     public function __construct() {
351         $relations = new \Doctrine\Common\Collections\ArrayCollection();
352     }
353
354     /**
355      *
356      * @return int
357      */
358     public function getId() {
359         return $this->id;
360     }
361
362     /**
363      *
364      * @param string $type
365      * @return void
366      */
367     public function setType($type) {
368         $this->type = $type;
369     }
370
371     /**
372      *
373      * @return string
374      */
375     public function getType() {
376         return $this->type;
377     }
378
379     /**
380      *
381      * @param string $abbreviation
382      * @return void
383      */
384     public function setAbbreviation($abbreviation) {
385         $this->abbreviation = $abbreviation;
386     }
387
388     /**
389      *
390      * @return string
391      */
392     public function getAbbreviation() {
393         return $this->abbreviation;
394     }
395
396     /**
397      *
398      * @param Relation $relation
399      * @return void
400      */
401     public function addRelation(Relation $relation) {
402         $this->relations[] = $relation;
403         $relation->setType($this);
404     }
405
406     /**
407      *
408      * @param Relation $relation
409      * @return void
410      */
411     public function removeRelation(Relation $relation) {
412         /*@var $removed Relation */
413         $removed = $this->relations->removeElement($relation);
414         if ($removed !== null) {
415             $removed->removeLemma();
416         }
417     }
418
419     /**
420      *
421      * @return kateglo\application\utilities\collections\ArrayCollection
422      */
423     public function getRelations() {
424         return $this->relations;
425     }
426 }
427