AdvancedAssociationTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  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. {
  12. protected function setUp() {
  13. parent::setUp();
  14. try {
  15. $this->_schemaTool->createSchema(array(
  16. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Phrase'),
  17. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\PhraseType'),
  18. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Definition'),
  19. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Lemma'),
  20. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Type')
  21. ));
  22. } catch (\Exception $e) {
  23. // Swallow all exceptions. We do not test the schema tool here.
  24. }
  25. }
  26. public function testIssue()
  27. {
  28. //setup
  29. $phrase = new Phrase;
  30. $phrase->setPhrase('lalala');
  31. $type = new PhraseType;
  32. $type->setType('nonsense');
  33. $type->setAbbreviation('non');
  34. $def1 = new Definition;
  35. $def1->setDefinition('def1');
  36. $def2 = new Definition;
  37. $def2->setDefinition('def2');
  38. $phrase->setType($type);
  39. $phrase->addDefinition($def1);
  40. $phrase->addDefinition($def2);
  41. $this->_em->persist($phrase);
  42. $this->_em->persist($type);
  43. $this->_em->flush();
  44. $this->_em->clear();
  45. //end setup
  46. // test1 - lazy-loading many-to-one after find()
  47. $phrase2 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
  48. $this->assertTrue(is_numeric($phrase2->getType()->getId()));
  49. $this->_em->clear();
  50. // test2 - eager load in DQL query
  51. $query = $this->_em->createQuery("SELECT p,t FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t");
  52. $res = $query->getResult();
  53. $this->assertEquals(1, count($res));
  54. $this->assertTrue($res[0]->getType() instanceof PhraseType);
  55. $this->assertTrue($res[0]->getType()->getPhrases() instanceof \Doctrine\ORM\PersistentCollection);
  56. $this->assertFalse($res[0]->getType()->getPhrases()->isInitialized());
  57. $this->_em->clear();
  58. // test2 - eager load in DQL query with double-join back and forth
  59. $query = $this->_em->createQuery("SELECT p,t,pp FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t JOIN t.phrases pp");
  60. $res = $query->getResult();
  61. $this->assertEquals(1, count($res));
  62. $this->assertTrue($res[0]->getType() instanceof PhraseType);
  63. $this->assertTrue($res[0]->getType()->getPhrases() instanceof \Doctrine\ORM\PersistentCollection);
  64. $this->assertTrue($res[0]->getType()->getPhrases()->isInitialized());
  65. $this->_em->clear();
  66. // test3 - lazy-loading one-to-many after find()
  67. $phrase3 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
  68. $definitions = $phrase3->getDefinitions();
  69. $this->assertTrue($definitions instanceof \Doctrine\ORM\PersistentCollection);
  70. $this->assertTrue($definitions[0] instanceof Definition);
  71. $this->_em->clear();
  72. // test4 - lazy-loading after DQL query
  73. $query = $this->_em->createQuery("SELECT p FROM Doctrine\Tests\ORM\Functional\Phrase p");
  74. $res = $query->getResult();
  75. $definitions = $res[0]->getDefinitions();
  76. $this->assertEquals(1, count($res));
  77. $this->assertTrue($definitions[0] instanceof Definition);
  78. $this->assertEquals(2, $definitions->count());
  79. }
  80. public function testManyToMany()
  81. {
  82. $lemma = new Lemma;
  83. $lemma->setLemma('abu');
  84. $type = new Type();
  85. $type->setType('nonsense');
  86. $type->setAbbreviation('non');
  87. $lemma->addType($type);
  88. $this->_em->persist($lemma);
  89. $this->_em->persist($type);
  90. $this->_em->flush();
  91. // test5 ManyToMany
  92. $query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Lemma l");
  93. $res = $query->getResult();
  94. $types = $res[0]->getTypes();
  95. $this->assertTrue($types[0] instanceof Type);
  96. }
  97. }
  98. /**
  99. * @Entity
  100. * @Table(name="lemma")
  101. */
  102. class Lemma {
  103. const CLASS_NAME = __CLASS__;
  104. /**
  105. * @var int
  106. * @Id
  107. * @Column(type="integer", name="lemma_id")
  108. * @GeneratedValue(strategy="AUTO")
  109. */
  110. private $id;
  111. /**
  112. *
  113. * @var string
  114. * @Column(type="string", name="lemma_name", unique=true, length=255)
  115. */
  116. private $lemma;
  117. /**
  118. * @var kateglo\application\utilities\collections\ArrayCollection
  119. * @ManyToMany(targetEntity="Type", mappedBy="lemmas", cascade={"persist"})
  120. */
  121. private $types;
  122. public function __construct() {
  123. $this->types = new \Doctrine\Common\Collections\ArrayCollection();
  124. }
  125. /**
  126. *
  127. * @return int
  128. */
  129. public function getId(){
  130. return $this->id;
  131. }
  132. /**
  133. *
  134. * @param string $lemma
  135. * @return void
  136. */
  137. public function setLemma($lemma){
  138. $this->lemma = $lemma;
  139. }
  140. /**
  141. *
  142. * @return string
  143. */
  144. public function getLemma(){
  145. return $this->lemma;
  146. }
  147. /**
  148. *
  149. * @param kateglo\application\models\Type $type
  150. * @return void
  151. */
  152. public function addType(Type $type){
  153. if (!$this->types->contains($type)) {
  154. $this->types[] = $type;
  155. $type->addLemma($this);
  156. }
  157. }
  158. /**
  159. *
  160. * @param kateglo\application\models\Type $type
  161. * @return void
  162. */
  163. public function removeType(Type $type)
  164. {
  165. $removed = $this->sources->removeElement($type);
  166. if ($removed !== null) {
  167. $removed->removeLemma($this);
  168. }
  169. }
  170. /**
  171. *
  172. * @return kateglo\application\helpers\collections\ArrayCollection
  173. */
  174. public function getTypes()
  175. {
  176. return $this->types;
  177. }
  178. }
  179. /**
  180. * @Entity
  181. * @Table(name="type")
  182. */
  183. class Type {
  184. const CLASS_NAME = __CLASS__;
  185. /**
  186. *
  187. * @var int
  188. * @Id
  189. * @Column(type="integer", name="type_id")
  190. * @GeneratedValue(strategy="AUTO")
  191. */
  192. private $id;
  193. /**
  194. *
  195. * @var string
  196. * @Column(type="string", name="type_name", unique=true)
  197. */
  198. private $type;
  199. /**
  200. *
  201. * @var string
  202. * @Column(type="string", name="type_abbreviation", unique=true)
  203. */
  204. private $abbreviation;
  205. /**
  206. * @var kateglo\application\helpers\collections\ArrayCollection
  207. * @ManyToMany(targetEntity="Lemma")
  208. * @JoinTable(name="lemma_type",
  209. * joinColumns={@JoinColumn(name="type_id", referencedColumnName="type_id")},
  210. * inverseJoinColumns={@JoinColumn(name="lemma_id", referencedColumnName="lemma_id")}
  211. * )
  212. */
  213. private $lemmas;
  214. public function __construct(){
  215. $this->lemmas = new \Doctrine\Common\Collections\ArrayCollection();
  216. }
  217. /**
  218. *
  219. * @return int
  220. */
  221. public function getId(){
  222. return $this->id;
  223. }
  224. /**
  225. *
  226. * @param string $type
  227. * @return void
  228. */
  229. public function setType($type){
  230. $this->type = $type;
  231. }
  232. /**
  233. *
  234. * @return string
  235. */
  236. public function getType(){
  237. return $this->type;
  238. }
  239. /**
  240. *
  241. * @param string $abbreviation
  242. * @return void
  243. */
  244. public function setAbbreviation($abbreviation){
  245. $this->abbreviation = $abbreviation;
  246. }
  247. /**
  248. *
  249. * @return string
  250. */
  251. public function getAbbreviation(){
  252. return $this->abbreviation;
  253. }
  254. /**
  255. *
  256. * @param kateglo\application\models\Lemma $lemma
  257. * @return void
  258. */
  259. public function addLemma(Lemma $lemma)
  260. {
  261. if (!$this->lemmas->contains($lemma)) {
  262. $this->lemmas[] = $lemma;
  263. $lemma->addType($this);
  264. }
  265. }
  266. /**
  267. *
  268. * @param kateglo\application\models\Lemma $lemma
  269. * @return void
  270. */
  271. public function removeLEmma(Lemma $lemma)
  272. {
  273. $removed = $this->lemmas->removeElement($lemma);
  274. if ($removed !== null) {
  275. $removed->removeType($this);
  276. }
  277. }
  278. /**
  279. *
  280. * @return kateglo\application\helpers\collections\ArrayCollection
  281. */
  282. public function getCategories()
  283. {
  284. return $this->categories;
  285. }
  286. }
  287. /**
  288. * @Entity
  289. * @Table(name="phrase")
  290. */
  291. class Phrase {
  292. const CLASS_NAME = __CLASS__;
  293. /**
  294. * @Id
  295. * @Column(type="integer", name="phrase_id")
  296. * @GeneratedValue(strategy="AUTO")
  297. */
  298. private $id;
  299. /**
  300. * @Column(type="string", name="phrase_name", unique=true, length=255)
  301. */
  302. private $phrase;
  303. /**
  304. * @ManyToOne(targetEntity="PhraseType")
  305. * @JoinColumn(name="phrase_type_id", referencedColumnName="phrase_type_id")
  306. */
  307. private $type;
  308. /**
  309. * @OneToMany(targetEntity="Definition", mappedBy="phrase", cascade={"persist"})
  310. */
  311. private $definitions;
  312. public function __construct() {
  313. $this->definitions = new \Doctrine\Common\Collections\ArrayCollection;
  314. }
  315. /**
  316. *
  317. * @param Definition $definition
  318. * @return void
  319. */
  320. public function addDefinition(Definition $definition){
  321. $this->definitions[] = $definition;
  322. $definition->setPhrase($this);
  323. }
  324. /**
  325. * @return int
  326. */
  327. public function getId(){
  328. return $this->id;
  329. }
  330. /**
  331. * @param string $phrase
  332. * @return void
  333. */
  334. public function setPhrase($phrase){
  335. $this->phrase = $phrase;
  336. }
  337. /**
  338. * @return string
  339. */
  340. public function getPhrase(){
  341. return $this->phrase;
  342. }
  343. /**
  344. *
  345. * @param PhraseType $type
  346. * @return void
  347. */
  348. public function setType(PhraseType $type){
  349. $this->type = $type;
  350. }
  351. /**
  352. *
  353. * @return PhraseType
  354. */
  355. public function getType(){
  356. return $this->type;
  357. }
  358. /**
  359. *
  360. * @return ArrayCollection
  361. */
  362. public function getDefinitions(){
  363. return $this->definitions;
  364. }
  365. }
  366. /**
  367. * @Entity
  368. * @Table(name="phrase_type")
  369. */
  370. class PhraseType {
  371. const CLASS_NAME = __CLASS__;
  372. /**
  373. * @Id
  374. * @Column(type="integer", name="phrase_type_id")
  375. * @GeneratedValue(strategy="AUTO")
  376. */
  377. private $id;
  378. /**
  379. * @Column(type="string", name="phrase_type_name", unique=true)
  380. */
  381. private $type;
  382. /**
  383. * @Column(type="string", name="phrase_type_abbreviation", unique=true)
  384. */
  385. private $abbreviation;
  386. /**
  387. * @OneToMany(targetEntity="Phrase", mappedBy="type")
  388. */
  389. private $phrases;
  390. public function __construct() {
  391. $this->phrases = new \Doctrine\Common\Collections\ArrayCollection;
  392. }
  393. /**
  394. * @return int
  395. */
  396. public function getId(){
  397. return $this->id;
  398. }
  399. /**
  400. * @param string $type
  401. * @return void
  402. */
  403. public function setType($type){
  404. $this->type = $type;
  405. }
  406. /**
  407. * @return string
  408. */
  409. public function getType(){
  410. return $this->type;
  411. }
  412. /**
  413. * @param string $abbreviation
  414. * @return void
  415. */
  416. public function setAbbreviation($abbreviation){
  417. $this->abbreviation = $abbreviation;
  418. }
  419. /**
  420. * @return string
  421. */
  422. public function getAbbreviation(){
  423. return $this->abbreviation;
  424. }
  425. /**
  426. * @param ArrayCollection $phrases
  427. * @return void
  428. */
  429. public function setPhrases($phrases){
  430. $this->phrases = $phrases;
  431. }
  432. /**
  433. *
  434. * @return ArrayCollection
  435. */
  436. public function getPhrases(){
  437. return $this->phrases;
  438. }
  439. }
  440. /**
  441. * @Entity
  442. * @Table(name="definition")
  443. */
  444. class Definition {
  445. const CLASS_NAME = __CLASS__;
  446. /**
  447. * @Id
  448. * @Column(type="integer", name="definition_id")
  449. * @GeneratedValue(strategy="AUTO")
  450. */
  451. private $id;
  452. /**
  453. * @ManyToOne(targetEntity="Phrase")
  454. * @JoinColumn(name="definition_phrase_id", referencedColumnName="phrase_id")
  455. */
  456. private $phrase;
  457. /**
  458. * @Column(type="text", name="definition_text")
  459. */
  460. private $definition;
  461. /**
  462. * @return int
  463. */
  464. public function getId(){
  465. return $this->id;
  466. }
  467. /**
  468. * @param Phrase $phrase
  469. * @return void
  470. */
  471. public function setPhrase(Phrase $phrase){
  472. $this->phrase = $phrase;
  473. }
  474. /**
  475. * @return Phrase
  476. */
  477. public function getPhrase(){
  478. return $this->phrase;
  479. }
  480. public function removePhrase() {
  481. if ($this->phrase !== null) {
  482. /*@var $phrase kateglo\application\models\Phrase */
  483. $phrase = $this->phrase;
  484. $this->phrase = null;
  485. $phrase->removeDefinition($this);
  486. }
  487. }
  488. /**
  489. * @param string $definition
  490. * @return void
  491. */
  492. public function setDefinition($definition){
  493. $this->definition = $definition;
  494. }
  495. /**
  496. * @return string
  497. */
  498. public function getDefinition(){
  499. return $this->definition;
  500. }
  501. }