TranslationQueryWalkerTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\ORM\Query;
  6. use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
  7. use Translatable\Fixture\Article;
  8. use Translatable\Fixture\Comment;
  9. /**
  10. * These are tests for translation query walker
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Translatable
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class TranslationQueryWalkerTest extends BaseTestCaseORM
  18. {
  19. const ARTICLE = 'Translatable\\Fixture\\Article';
  20. const COMMENT = 'Translatable\\Fixture\\Comment';
  21. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  22. const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
  23. private $translationListener;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $evm = new EventManager;
  28. $this->translationListener = new TranslationListener();
  29. $this->translationListener->setTranslatableLocale('en_us');
  30. $this->translationListener->setDefaultLocale('en_us');
  31. $evm->addEventSubscriber($this->translationListener);
  32. $this->getMockSqliteEntityManager($evm);
  33. $this->populate();
  34. }
  35. public function testSubselectByTranslatedField()
  36. {
  37. $this->populateMore();
  38. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  39. $subSelect = 'SELECT a2.title FROM ' . self::ARTICLE . ' a2';
  40. $subSelect .= " WHERE a2.title LIKE '%ab%'";
  41. $dql .= " WHERE a.title IN ({$subSelect})";
  42. $dql .= ' ORDER BY a.title';
  43. $q = $this->em->createQuery($dql);
  44. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  45. // array hydration
  46. $this->translationListener->setTranslatableLocale('en_us');
  47. $result = $q->getArrayResult();
  48. $this->assertEquals(2, count($result));
  49. $this->assertEquals('Alfabet', $result[0]['title']);
  50. $this->assertEquals('Cabbages', $result[1]['title']);
  51. }
  52. public function testSubselectStatements()
  53. {
  54. $this->populateMore();
  55. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  56. $subSelect = 'SELECT a2.id FROM ' . self::ARTICLE . ' a2';
  57. $subSelect .= " WHERE a2.title LIKE '%ab%'";
  58. $dql .= " WHERE a.id IN ({$subSelect})";
  59. $dql .= ' ORDER BY a.title';
  60. $q = $this->em->createQuery($dql);
  61. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  62. // array hydration
  63. $this->translationListener->setTranslatableLocale('en_us');
  64. $result = $q->getArrayResult();
  65. $this->assertEquals(2, count($result));
  66. $this->assertEquals('Alfabet', $result[0]['title']);
  67. $this->assertEquals('Cabbages', $result[1]['title']);
  68. }
  69. public function testJoinedWithStatements()
  70. {
  71. $this->populateMore();
  72. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  73. //@todo: its impossible to support translated values in WITH statement
  74. $dql .= ' LEFT JOIN a.comments c WITH c.subject LIKE :lookup';
  75. $dql .= ' WHERE a.title LIKE :filter';
  76. $dql .= ' ORDER BY a.title';
  77. $q = $this->em->createQuery($dql);
  78. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  79. // array hydration
  80. $this->translationListener->setTranslatableLocale('en_us');
  81. $q->setParameter('lookup', '%ger%');
  82. $q->setParameter('filter', 'Foo%');
  83. $result = $q->getArrayResult();
  84. $this->assertEquals(1, count($result));
  85. $this->assertEquals('Food', $result[0]['title']);
  86. $comments = $result[0]['comments'];
  87. $this->assertEquals(1, count($comments));
  88. $this->assertEquals('good', $comments[0]['subject']);
  89. }
  90. public function testSelectWithTranslationFallbackOnSimpleObjectHydration()
  91. {
  92. $this->em
  93. ->getConfiguration()
  94. ->expects($this->any())
  95. ->method('getCustomHydrationMode')
  96. ->with(TranslationWalker::HYDRATE_SIMPLE_OBJECT_TRANSLATION)
  97. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\SimpleObjectHydrator'));
  98. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  99. $q = $this->em->createQuery($dql);
  100. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  101. $this->translationListener->setTranslatableLocale('ru_ru');
  102. $this->translationListener->setTranslationFallback(false);
  103. // simple object hydration
  104. $this->startQueryLog();
  105. $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
  106. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  107. $this->assertEquals('', $result[0]->getTitle());
  108. $this->assertEquals('', $result[0]->getContent());
  109. $this->translationListener->setTranslationFallback(true);
  110. $this->queryAnalyzer->cleanUp();
  111. $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
  112. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  113. //Default translation is en_us, so we expect the results in that locale
  114. $this->assertEquals('Food', $result[0]->getTitle());
  115. $this->assertEquals('about food', $result[0]->getContent());
  116. }
  117. public function testSelectWithTranslationFallbackOnArrayHydration()
  118. {
  119. $this->em
  120. ->getConfiguration()
  121. ->expects($this->any())
  122. ->method('getCustomHydrationMode')
  123. ->with(TranslationWalker::HYDRATE_ARRAY_TRANSLATION)
  124. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ArrayHydrator'));
  125. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  126. $dql .= ' LEFT JOIN a.comments c';
  127. $q = $this->em->createQuery($dql);
  128. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  129. $this->translationListener->setTranslatableLocale('ru_ru');
  130. $this->translationListener->setTranslationFallback(false);
  131. // array hydration
  132. $this->startQueryLog();
  133. $result = $q->getArrayResult();
  134. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  135. $this->assertEquals('', $result[0]['title']);
  136. $this->assertEquals('', $result[0]['content']);
  137. $this->translationListener->setTranslationFallback(true);
  138. $this->queryAnalyzer->cleanUp();
  139. $result = $q->getArrayResult();
  140. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  141. //Default translation is en_us, so we expect the results in that locale
  142. $this->assertEquals('Food', $result[0]['title']);
  143. $this->assertEquals('about food', $result[0]['content']);
  144. }
  145. public function testSelectWithTranslationFallbackOnObjectHydration()
  146. {
  147. $this->em
  148. ->getConfiguration()
  149. ->expects($this->any())
  150. ->method('getCustomHydrationMode')
  151. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  152. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  153. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  154. $q = $this->em->createQuery($dql);
  155. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  156. $this->translationListener->setTranslatableLocale('ru_ru');
  157. $this->translationListener->setTranslationFallback(false);
  158. // object hydration
  159. $this->startQueryLog();
  160. $result = $q->getResult();
  161. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  162. $this->assertEquals('', $result[0]->getTitle());
  163. $this->assertEquals('', $result[0]->getContent());
  164. $this->translationListener->setTranslationFallback(true);
  165. $this->queryAnalyzer->cleanUp();
  166. $result = $q->getResult();
  167. $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
  168. //Default translation is en_us, so we expect the results in that locale
  169. $this->assertEquals('Food', $result[0]->getTitle());
  170. $this->assertEquals('about food', $result[0]->getContent());
  171. }
  172. public function testSelectCountStatement()
  173. {
  174. $dql = 'SELECT COUNT(a) FROM ' . self::ARTICLE . ' a';
  175. $dql .= ' WHERE a.title LIKE :title';
  176. $q = $this->em->createQuery($dql);
  177. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  178. $this->translationListener->setTranslatableLocale('en_us');
  179. $q->setParameter('title', 'Foo%');
  180. $result = $q->getSingleScalarResult();
  181. $this->assertEquals(1, $result);
  182. $this->translationListener->setTranslatableLocale('lt_lt');
  183. $q->setParameter('title', 'Mai%');
  184. $result = $q->getSingleScalarResult();
  185. $this->assertEquals(1, $result);
  186. $this->translationListener->setTranslatableLocale('en_us');
  187. $q->setParameter('title', 'Mai%');
  188. $result = $q->getSingleScalarResult();
  189. $this->assertEquals(0, $result);
  190. }
  191. public function testSelectOrderedJoinedComponentTranslation()
  192. {
  193. $this->em
  194. ->getConfiguration()
  195. ->expects($this->any())
  196. ->method('getCustomHydrationMode')
  197. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  198. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  199. $this->populateMore();
  200. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  201. $dql .= ' LEFT JOIN a.comments c';
  202. $dql .= ' ORDER BY a.title';
  203. $q = $this->em->createQuery($dql);
  204. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  205. // array hydration
  206. $this->translationListener->setTranslatableLocale('en_us');
  207. $result = $q->getArrayResult();
  208. $this->assertEquals(4, count($result));
  209. $this->assertEquals('Alfabet', $result[0]['title']);
  210. $this->assertEquals('Cabbages', $result[1]['title']);
  211. $this->assertEquals('Food', $result[2]['title']);
  212. $this->assertEquals('Woman', $result[3]['title']);
  213. $this->translationListener->setTranslatableLocale('lt_lt');
  214. $result = $q->getArrayResult();
  215. $this->assertEquals(4, count($result));
  216. $this->assertEquals('Alfabetas', $result[0]['title']);
  217. $this->assertEquals('Kopustai', $result[1]['title']);
  218. $this->assertEquals('Maistas', $result[2]['title']);
  219. $this->assertEquals('Moteris', $result[3]['title']);
  220. // object hydration
  221. $this->translationListener->setTranslatableLocale('en_us');
  222. $result = $q->getResult();
  223. $this->assertEquals(4, count($result));
  224. $this->assertEquals('Alfabet', $result[0]->getTitle());
  225. $this->assertEquals('Cabbages', $result[1]->getTitle());
  226. $this->assertEquals('Food', $result[2]->getTitle());
  227. $this->assertEquals('Woman', $result[3]->getTitle());
  228. $this->translationListener->setTranslatableLocale('lt_lt');
  229. $result = $q->getResult();
  230. $this->assertEquals(4, count($result));
  231. $this->assertEquals('Alfabetas', $result[0]->getTitle());
  232. $this->assertEquals('Kopustai', $result[1]->getTitle());
  233. $this->assertEquals('Maistas', $result[2]->getTitle());
  234. $this->assertEquals('Moteris', $result[3]->getTitle());
  235. }
  236. public function testSelectSecondJoinedComponentTranslation()
  237. {
  238. $this->em
  239. ->getConfiguration()
  240. ->expects($this->any())
  241. ->method('getCustomHydrationMode')
  242. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  243. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  244. $dql = 'SELECT a, c FROM ' . self::ARTICLE . ' a';
  245. $dql .= ' LEFT JOIN a.comments c';
  246. $q = $this->em->createQuery($dql);
  247. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  248. // array hydration
  249. $this->translationListener->setTranslatableLocale('en_us');
  250. $result = $q->getArrayResult();
  251. $this->assertEquals(1, count($result));
  252. $food = $result[0];
  253. $this->assertEquals(4, count($food));
  254. $this->assertEquals('Food', $food['title']);
  255. $this->assertEquals('about food', $food['content']);
  256. $comments = $food['comments'];
  257. $this->assertEquals(2, count($comments));
  258. $good = $comments[0];
  259. $this->assertEquals(3, count($good));
  260. $this->assertEquals('good', $good['subject']);
  261. $this->assertEquals('food is good', $good['message']);
  262. $bad = $comments[1];
  263. $this->assertEquals(3, count($bad));
  264. $this->assertEquals('bad', $bad['subject']);
  265. $this->assertEquals('food is bad', $bad['message']);
  266. $this->translationListener->setTranslatableLocale('lt_lt');
  267. $result = $q->getArrayResult();
  268. $this->assertEquals(1, count($result));
  269. $food = $result[0];
  270. $this->assertEquals(4, count($food));
  271. $this->assertEquals('Maistas', $food['title']);
  272. $this->assertEquals('apie maista', $food['content']);
  273. $comments = $food['comments'];
  274. $this->assertEquals(2, count($comments));
  275. $good = $comments[0];
  276. $this->assertEquals(3, count($good));
  277. $this->assertEquals('geras', $good['subject']);
  278. $this->assertEquals('maistas yra geras', $good['message']);
  279. $bad = $comments[1];
  280. $this->assertEquals(3, count($bad));
  281. $this->assertEquals('blogas', $bad['subject']);
  282. $this->assertEquals('maistas yra blogas', $bad['message']);
  283. // object hydration
  284. $this->translationListener->setTranslatableLocale('en_us');
  285. $result = $q->getResult();
  286. $this->assertEquals(1, count($result));
  287. $food = $result[0];
  288. $this->assertEquals('Food', $food->getTitle());
  289. $this->assertEquals('about food', $food->getContent());
  290. $comments = $food->getComments();
  291. $this->assertEquals(2, count($comments));
  292. $good = $comments[0];
  293. $this->assertEquals('good', $good->getSubject());
  294. $this->assertEquals('food is good', $good->getMessage());
  295. $bad = $comments[1];
  296. $this->assertEquals('bad', $bad->getSubject());
  297. $this->assertEquals('food is bad', $bad->getMessage());
  298. $this->translationListener->setTranslatableLocale('lt_lt');
  299. $result = $q->getResult();
  300. $this->assertEquals(1, count($result));
  301. $food = $result[0];
  302. $this->assertEquals('Maistas', $food->getTitle());
  303. $this->assertEquals('apie maista', $food->getContent());
  304. $comments = $food->getComments();
  305. $this->assertEquals(2, count($comments));
  306. $good = $comments[0];
  307. $this->assertInstanceOf(self::COMMENT, $good);
  308. $this->assertEquals('geras', $good->getSubject());
  309. $this->assertEquals('maistas yra geras', $good->getMessage());
  310. $bad = $comments[1];
  311. $this->assertEquals('blogas', $bad->getSubject());
  312. $this->assertEquals('maistas yra blogas', $bad->getMessage());
  313. }
  314. public function testSelectSinglePartializedComponentTranslation()
  315. {
  316. $this->em
  317. ->getConfiguration()
  318. ->expects($this->any())
  319. ->method('getCustomHydrationMode')
  320. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  321. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  322. $dql = 'SELECT a.title FROM ' . self::ARTICLE . ' a';
  323. $q = $this->em->createQuery($dql);
  324. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  325. // array hydration
  326. $this->translationListener->setTranslatableLocale('en_us');
  327. $result = $q->getArrayResult();
  328. $this->assertEquals(1, count($result));
  329. $food = $result[0];
  330. $this->assertEquals(1, count($food));
  331. $this->assertEquals('Food', $food['title']);
  332. $this->translationListener->setTranslatableLocale('lt_lt');
  333. $result = $q->getArrayResult();
  334. $this->assertEquals(1, count($result));
  335. $food = $result[0];
  336. $this->assertEquals(1, count($food));
  337. $this->assertEquals('Maistas', $food['title']);
  338. // object hydration
  339. $this->translationListener->setTranslatableLocale('en_us');
  340. $result = $q->getResult();
  341. $this->assertEquals(1, count($result));
  342. $food = $result[0];
  343. $this->assertEquals(1, count($food));
  344. $this->assertEquals('Food', $food['title']);
  345. $this->translationListener->setTranslatableLocale('lt_lt');
  346. $result = $q->getResult();
  347. $this->assertEquals(1, count($result));
  348. $food = $result[0];
  349. $this->assertEquals(1, count($food));
  350. $this->assertEquals('Maistas', $food['title']);
  351. }
  352. public function testSelectSingleComponentTranslation()
  353. {
  354. $this->em
  355. ->getConfiguration()
  356. ->expects($this->any())
  357. ->method('getCustomHydrationMode')
  358. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  359. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'));
  360. $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
  361. $q = $this->em->createQuery($dql);
  362. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  363. // array hydration
  364. $this->translationListener->setTranslatableLocale('en_us');
  365. $result = $q->getArrayResult();
  366. $this->assertEquals(1, count($result));
  367. $food = $result[0];
  368. $this->assertEquals(3, count($food));
  369. $this->assertEquals('Food', $food['title']);
  370. $this->assertEquals('about food', $food['content']);
  371. $this->translationListener->setTranslatableLocale('lt_lt');
  372. $result = $q->getArrayResult();
  373. $this->assertEquals(1, count($result));
  374. $food = $result[0];
  375. $this->assertEquals(3, count($food));
  376. $this->assertEquals('Maistas', $food['title']);
  377. $this->assertEquals('apie maista', $food['content']);
  378. // object hydration
  379. $this->translationListener->setTranslatableLocale('en_us');
  380. $result = $q->getResult();
  381. $this->assertEquals(1, count($result));
  382. $food = $result[0];
  383. $this->assertInstanceOf(self::ARTICLE, $food);
  384. $this->assertEquals('Food', $food->getTitle());
  385. $this->assertEquals('about food', $food->getContent());
  386. $this->translationListener->setTranslatableLocale('lt_lt');
  387. $result = $q->getResult();
  388. $this->assertEquals(1, count($result));
  389. $food = $result[0];
  390. $this->assertEquals('Maistas', $food->getTitle());
  391. $this->assertEquals('apie maista', $food->getContent());
  392. }
  393. /**
  394. * @group testSelectWithUnmappedField
  395. */
  396. public function testSelectWithUnmappedField()
  397. {
  398. $dql = 'SELECT a.title, count(a.id) AS num FROM ' . self::ARTICLE . ' a';
  399. $dql .= ' ORDER BY a.title';
  400. $q = $this->em->createQuery($dql);
  401. $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
  402. // array hydration
  403. $this->translationListener->setTranslatableLocale('en_us');
  404. $result = $q->getArrayResult();
  405. $this->assertEquals(1, count($result));
  406. $this->assertEquals('Food', $result[0]['title']);
  407. $this->assertEquals(1, $result[0]['num']);
  408. }
  409. protected function getUsedEntityFixtures()
  410. {
  411. return array(
  412. self::ARTICLE,
  413. self::TRANSLATION,
  414. self::COMMENT
  415. );
  416. }
  417. private function populateMore()
  418. {
  419. $repo = $this->em->getRepository(self::ARTICLE);
  420. $commentRepo = $this->em->getRepository(self::COMMENT);
  421. $this->translationListener->setTranslatableLocale('en_us');
  422. $alfabet = new Article;
  423. $alfabet->setTitle('Alfabet');
  424. $alfabet->setContent('hey wtf!');
  425. $woman = new Article;
  426. $woman->setTitle('Woman');
  427. $woman->setContent('i like them');
  428. $cabbages = new Article;
  429. $cabbages->setTitle('Cabbages');
  430. $cabbages->setContent('where went the woman?');
  431. $this->em->persist($alfabet);
  432. $this->em->persist($woman);
  433. $this->em->persist($cabbages);
  434. $this->em->flush();
  435. $this->em->clear();
  436. $this->translationListener->setTranslatableLocale('lt_lt');
  437. $alfabet = $repo->find(2);
  438. $alfabet->setTitle('Alfabetas');
  439. $alfabet->setContent('ei wtf!');
  440. $woman = $repo->find(3);
  441. $woman->setTitle('Moteris');
  442. $woman->setContent('as megstu jas');
  443. $cabbages = $repo->find(4);
  444. $cabbages->setTitle('Kopustai');
  445. $cabbages->setContent('kur dingo moteris?');
  446. $this->em->persist($alfabet);
  447. $this->em->persist($woman);
  448. $this->em->persist($cabbages);
  449. $this->em->flush();
  450. $this->em->clear();
  451. }
  452. private function populate()
  453. {
  454. $repo = $this->em->getRepository(self::ARTICLE);
  455. $commentRepo = $this->em->getRepository(self::COMMENT);
  456. $food = new Article;
  457. $food->setTitle('Food');
  458. $food->setContent('about food');
  459. $goodFood = new Comment;
  460. $goodFood->setArticle($food);
  461. $goodFood->setMessage('food is good');
  462. $goodFood->setSubject('good');
  463. $badFood = new Comment;
  464. $badFood->setArticle($food);
  465. $badFood->setMessage('food is bad');
  466. $badFood->setSubject('bad');
  467. $this->em->persist($food);
  468. $this->em->persist($goodFood);
  469. $this->em->persist($badFood);
  470. $this->em->flush();
  471. $this->em->clear();
  472. $this->translationListener->setTranslatableLocale('lt_lt');
  473. $food = $repo->find(1);
  474. $food->setTitle('Maistas');
  475. $food->setContent('apie maista');
  476. $goodFood = $commentRepo->find(1);
  477. $goodFood->setArticle($food);
  478. $goodFood->setMessage('maistas yra geras');
  479. $goodFood->setSubject('geras');
  480. $badFood = $commentRepo->find(2);
  481. $badFood->setArticle($food);
  482. $badFood->setMessage('maistas yra blogas');
  483. $badFood->setSubject('blogas');
  484. $this->em->persist($food);
  485. $this->em->persist($goodFood);
  486. $this->em->persist($badFood);
  487. $this->em->flush();
  488. $this->em->clear();
  489. }
  490. }