PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/tests/Doctrine/Tests/ORM/Functional/Ticket/Ticket69.php

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