3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\ORM\Query;
7 require_once __DIR__ . '/../../TestInit.php';
10 * Functional tests for the Single Table Inheritance mapping strategy.
14 class AdvancedAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
16 protected function setUp() {
19 $this->_schemaTool->createSchema(array(
20 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Phrase'),
21 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\PhraseType'),
22 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Definition'),
23 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Lemma'),
24 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Type')
26 } catch (\Exception $e) {
27 // Swallow all exceptions. We do not test the schema tool here.
31 public function testIssue()
35 $phrase->setPhrase('lalala');
37 $type = new PhraseType;
38 $type->setType('nonsense');
39 $type->setAbbreviation('non');
41 $def1 = new Definition;
42 $def1->setDefinition('def1');
43 $def2 = new Definition;
44 $def2->setDefinition('def2');
46 $phrase->setType($type);
47 $phrase->addDefinition($def1);
48 $phrase->addDefinition($def2);
50 $this->_em->persist($phrase);
51 $this->_em->persist($type);
57 // test1 - lazy-loading many-to-one after find()
58 $phrase2 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
59 $this->assertTrue(is_numeric($phrase2->getType()->getId()));
63 // test2 - eager load in DQL query
64 $query = $this->_em->createQuery("SELECT p,t FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t");
65 $res = $query->getResult();
66 $this->assertEquals(1, count($res));
67 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\PhraseType', $res[0]->getType());
68 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $res[0]->getType()->getPhrases());
69 $this->assertFalse($res[0]->getType()->getPhrases()->isInitialized());
73 // test2 - eager load in DQL query with double-join back and forth
74 $query = $this->_em->createQuery("SELECT p,t,pp FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t JOIN t.phrases pp");
75 $res = $query->getResult();
76 $this->assertEquals(1, count($res));
77 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\PhraseType', $res[0]->getType());
78 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $res[0]->getType()->getPhrases());
79 $this->assertTrue($res[0]->getType()->getPhrases()->isInitialized());
83 // test3 - lazy-loading one-to-many after find()
84 $phrase3 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
85 $definitions = $phrase3->getDefinitions();
86 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $definitions);
87 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Definition', $definitions[0]);
91 // test4 - lazy-loading after DQL query
92 $query = $this->_em->createQuery("SELECT p FROM Doctrine\Tests\ORM\Functional\Phrase p");
93 $res = $query->getResult();
94 $definitions = $res[0]->getDefinitions();
96 $this->assertEquals(1, count($res));
98 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Definition', $definitions[0]);
99 $this->assertEquals(2, $definitions->count());
102 public function testManyToMany()
105 $lemma->setLemma('abu');
108 $type->setType('nonsense');
109 $type->setAbbreviation('non');
111 $lemma->addType($type);
113 $this->_em->persist($lemma);
114 $this->_em->persist($type);
118 $query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Lemma l");
119 $res = $query->getResult();
120 $types = $res[0]->getTypes();
122 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Type', $types[0]);
128 * @Table(name="lemma")
132 const CLASS_NAME = __CLASS__;
137 * @Column(type="integer", name="lemma_id")
138 * @GeneratedValue(strategy="AUTO")
145 * @Column(type="string", name="lemma_name", unique=true, length=255)
150 * @var kateglo\application\utilities\collections\ArrayCollection
151 * @ManyToMany(targetEntity="Type", mappedBy="lemmas", cascade={"persist"})
155 public function __construct() {
156 $this->types = new \Doctrine\Common\Collections\ArrayCollection();
164 public function getId(){
170 * @param string $lemma
173 public function setLemma($lemma){
174 $this->lemma = $lemma;
181 public function getLemma(){
187 * @param kateglo\application\models\Type $type
190 public function addType(Type $type){
191 if (!$this->types->contains($type)) {
192 $this->types[] = $type;
193 $type->addLemma($this);
199 * @param kateglo\application\models\Type $type
202 public function removeType(Type $type)
204 $removed = $this->sources->removeElement($type);
205 if ($removed !== null) {
206 $removed->removeLemma($this);
212 * @return kateglo\application\helpers\collections\ArrayCollection
214 public function getTypes()
222 * @Table(name="type")
226 const CLASS_NAME = __CLASS__;
232 * @Column(type="integer", name="type_id")
233 * @GeneratedValue(strategy="AUTO")
240 * @Column(type="string", name="type_name", unique=true)
247 * @Column(type="string", name="type_abbreviation", unique=true)
249 private $abbreviation;
252 * @var kateglo\application\helpers\collections\ArrayCollection
253 * @ManyToMany(targetEntity="Lemma")
254 * @JoinTable(name="lemma_type",
255 * joinColumns={@JoinColumn(name="type_id", referencedColumnName="type_id")},
256 * inverseJoinColumns={@JoinColumn(name="lemma_id", referencedColumnName="lemma_id")}
261 public function __construct(){
262 $this->lemmas = new \Doctrine\Common\Collections\ArrayCollection();
269 public function getId(){
275 * @param string $type
278 public function setType($type){
286 public function getType(){
292 * @param string $abbreviation
295 public function setAbbreviation($abbreviation){
296 $this->abbreviation = $abbreviation;
303 public function getAbbreviation(){
304 return $this->abbreviation;
309 * @param kateglo\application\models\Lemma $lemma
312 public function addLemma(Lemma $lemma)
314 if (!$this->lemmas->contains($lemma)) {
315 $this->lemmas[] = $lemma;
316 $lemma->addType($this);
322 * @param kateglo\application\models\Lemma $lemma
325 public function removeLEmma(Lemma $lemma)
327 $removed = $this->lemmas->removeElement($lemma);
328 if ($removed !== null) {
329 $removed->removeType($this);
335 * @return kateglo\application\helpers\collections\ArrayCollection
337 public function getCategories()
339 return $this->categories;
347 * @Table(name="phrase")
351 const CLASS_NAME = __CLASS__;
355 * @Column(type="integer", name="phrase_id")
356 * @GeneratedValue(strategy="AUTO")
361 * @Column(type="string", name="phrase_name", unique=true, length=255)
366 * @ManyToOne(targetEntity="PhraseType")
367 * @JoinColumn(name="phrase_type_id", referencedColumnName="phrase_type_id")
372 * @OneToMany(targetEntity="Definition", mappedBy="phrase", cascade={"persist"})
374 private $definitions;
376 public function __construct() {
377 $this->definitions = new \Doctrine\Common\Collections\ArrayCollection;
382 * @param Definition $definition
385 public function addDefinition(Definition $definition){
386 $this->definitions[] = $definition;
387 $definition->setPhrase($this);
393 public function getId(){
398 * @param string $phrase
401 public function setPhrase($phrase){
402 $this->phrase = $phrase;
408 public function getPhrase(){
409 return $this->phrase;
414 * @param PhraseType $type
417 public function setType(PhraseType $type){
425 public function getType(){
431 * @return ArrayCollection
433 public function getDefinitions(){
434 return $this->definitions;
440 * @Table(name="phrase_type")
444 const CLASS_NAME = __CLASS__;
448 * @Column(type="integer", name="phrase_type_id")
449 * @GeneratedValue(strategy="AUTO")
454 * @Column(type="string", name="phrase_type_name", unique=true)
459 * @Column(type="string", name="phrase_type_abbreviation", unique=true)
461 private $abbreviation;
464 * @OneToMany(targetEntity="Phrase", mappedBy="type")
468 public function __construct() {
469 $this->phrases = new \Doctrine\Common\Collections\ArrayCollection;
475 public function getId(){
480 * @param string $type
483 public function setType($type){
490 public function getType(){
495 * @param string $abbreviation
498 public function setAbbreviation($abbreviation){
499 $this->abbreviation = $abbreviation;
505 public function getAbbreviation(){
506 return $this->abbreviation;
510 * @param ArrayCollection $phrases
513 public function setPhrases($phrases){
514 $this->phrases = $phrases;
519 * @return ArrayCollection
521 public function getPhrases(){
522 return $this->phrases;
529 * @Table(name="definition")
533 const CLASS_NAME = __CLASS__;
537 * @Column(type="integer", name="definition_id")
538 * @GeneratedValue(strategy="AUTO")
543 * @ManyToOne(targetEntity="Phrase")
544 * @JoinColumn(name="definition_phrase_id", referencedColumnName="phrase_id")
549 * @Column(type="text", name="definition_text")
556 public function getId(){
561 * @param Phrase $phrase
564 public function setPhrase(Phrase $phrase){
565 $this->phrase = $phrase;
571 public function getPhrase(){
572 return $this->phrase;
575 public function removePhrase() {
576 if ($this->phrase !== null) {
577 /*@var $phrase kateglo\application\models\Phrase */
578 $phrase = $this->phrase;
579 $this->phrase = null;
580 $phrase->removeDefinition($this);
585 * @param string $definition
588 public function setDefinition($definition){
589 $this->definition = $definition;
595 public function getDefinition(){
596 return $this->definition;