3 namespace Doctrine\Tests\ORM\Functional\Ticket;
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 {
15 protected function setUp()
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')
24 } catch (\Exception $e) {
25 // Swallow all exceptions. We do not test the schema tool here.
29 public function testIssue()
33 $lemma1->setLemma('foo');
36 $lemma2->setLemma('bar');
39 $lemma3->setLemma('batz');
42 $lemma4->setLemma('bla');
44 $type1 = new RelationType;
45 $type1->setType('nonsense');
46 $type1->setAbbreviation('non');
48 $type2 = new RelationType;
49 $type2->setType('quatsch');
50 $type2->setAbbreviation('qu');
52 $relation1 = new Relation;
53 $relation1->setParent($lemma1);
54 $relation1->setChild($lemma2);
55 $relation1->setType($type1);
57 $relation2 = new Relation;
58 $relation2->setParent($lemma1);
59 $relation2->setChild($lemma3);
60 $relation2->setType($type1);
62 $relation3 = new Relation;
63 $relation3->setParent($lemma1);
64 $relation3->setChild($lemma4);
65 $relation3->setType($type2);
67 $lemma1->addRelation($relation1);
68 $lemma1->addRelation($relation2);
69 $lemma1->addRelation($relation3);
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);
83 $query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Ticket\Lemma l Where l.lemma = 'foo'");
84 $res = $query->getResult();
87 $this->assertEquals('foo', $lemma->getLemma());
88 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\Lemma', $lemma);
89 $relations = $lemma->getRelations();
91 foreach($relations as $relation) {
92 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\Relation', $relation);
93 $this->assertTrue($relation->getType()->getType() != '');
103 * @Table(name="lemma")
107 const CLASS_NAME = __CLASS__;
112 * @Column(type="integer", name="lemma_id")
113 * @GeneratedValue(strategy="AUTO")
120 * @Column(type="string", name="lemma_name", unique=true, length=255)
126 * @var kateglo\application\utilities\collections\ArrayCollection
127 * @OneToMany(targetEntity="Relation", mappedBy="parent", cascade={"persist"})
131 public function __construct() {
132 $this->types = new \Doctrine\Common\Collections\ArrayCollection();
133 $this->relations = new \Doctrine\Common\Collections\ArrayCollection();
141 public function getId() {
147 * @param string $lemma
150 public function setLemma($lemma) {
151 $this->lemma = $lemma;
158 public function getLemma() {
165 * @param Relation $relation
168 public function addRelation(Relation $relation) {
169 $this->relations[] = $relation;
170 $relation->setParent($this);
175 * @param Relation $relation
178 public function removeRelation(Relation $relation) {
179 /*@var $removed Relation */
180 $removed = $this->relations->removeElement($relation);
181 if ($removed !== null) {
182 $removed->removeParent();
188 * @return kateglo\application\utilities\collections\ArrayCollection
190 public function getRelations() {
191 return $this->relations;
199 * @Table(name="relation")
203 const CLASS_NAME = __CLASS__;
208 * @Column(type="integer", name="relation_id")
209 * @GeneratedValue(strategy="AUTO")
215 * @ManyToOne(targetEntity="Lemma", inversedBy="relations")
216 * @JoinColumn(name="relation_parent_id", referencedColumnName="lemma_id")
222 * @OneToOne(targetEntity="Lemma")
223 * @JoinColumn(name="relation_child_id", referencedColumnName="lemma_id")
229 * @ManyToOne(targetEntity="RelationType", inversedBy="relations")
230 * @JoinColumn(name="relation_type_id", referencedColumnName="relation_type_id")
236 * @param Lemma $parent
239 public function setParent(Lemma $parent) {
240 $this->parent = $parent;
247 public function getParent() {
248 return $this->parent;
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);
266 * @param Lemma $child
269 public function setChild(Lemma $child) {
270 $this->child = $child;
277 public function getChild() {
283 * @param RelationType $type
286 public function setType(RelationType $type) {
292 * @return RelationType
294 public function getType() {
302 public function removeType() {
303 if ($this->type !== null) {
304 /*@var $phrase RelationType */
307 $type->removeRelation($this);
315 * @Table(name="relation_type")
319 const CLASS_NAME = __CLASS__;
325 * @Column(type="integer", name="relation_type_id")
326 * @GeneratedValue(strategy="AUTO")
333 * @Column(type="string", name="relation_type_name", unique=true, length=255)
340 * @Column(type="string", name="relation_type_abbreviation", unique=true, length=255)
342 private $abbreviation;
345 * @var kateglo\application\utilities\collections\ArrayCollection
346 * @OneToMany(targetEntity="Relation", mappedBy="type", cascade={"persist"})
350 public function __construct() {
351 $relations = new \Doctrine\Common\Collections\ArrayCollection();
358 public function getId() {
364 * @param string $type
367 public function setType($type) {
375 public function getType() {
381 * @param string $abbreviation
384 public function setAbbreviation($abbreviation) {
385 $this->abbreviation = $abbreviation;
392 public function getAbbreviation() {
393 return $this->abbreviation;
398 * @param Relation $relation
401 public function addRelation(Relation $relation) {
402 $this->relations[] = $relation;
403 $relation->setType($this);
408 * @param Relation $relation
411 public function removeRelation(Relation $relation) {
412 /*@var $removed Relation */
413 $removed = $this->relations->removeElement($relation);
414 if ($removed !== null) {
415 $removed->removeLemma();
421 * @return kateglo\application\utilities\collections\ArrayCollection
423 public function getRelations() {
424 return $this->relations;