DDC767Test.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Tests\Models\CMS\CmsUser;
  5. use Doctrine\Tests\Models\CMS\CmsGroup;
  6. require_once __DIR__ . '/../../../TestInit.php';
  7. class DDC767Test extends \Doctrine\Tests\OrmFunctionalTestCase
  8. {
  9. protected function setUp()
  10. {
  11. $this->useModelSet('cms');
  12. parent::setUp();
  13. }
  14. /**
  15. * @group DDC-767
  16. */
  17. public function testCollectionChangesInsideTransaction()
  18. {
  19. $user = new CmsUser();
  20. $user->name = "beberlei";
  21. $user->status = "active";
  22. $user->username = "beberlei";
  23. $group1 = new CmsGroup();
  24. $group1->name = "foo";
  25. $group2 = new CmsGroup();
  26. $group2->name = "bar";
  27. $group3 = new CmsGroup();
  28. $group3->name = "baz";
  29. $user->addGroup($group1);
  30. $user->addGroup($group2);
  31. $this->_em->persist($user);
  32. $this->_em->persist($group1);
  33. $this->_em->persist($group2);
  34. $this->_em->persist($group3);
  35. $this->_em->flush();
  36. $this->_em->clear();
  37. /* @var $pUser CmsUser */
  38. $pUser = $this->_em->find(get_class($user), $user->id);
  39. $this->assertNotNull($pUser, "User not retrieved from database.");
  40. $groups = array(2, 3);
  41. try {
  42. $this->_em->beginTransaction();
  43. $pUser->groups->clear();
  44. $this->_em->flush();
  45. // Add new
  46. foreach ($groups as $groupId) {
  47. $pUser->addGroup($this->_em->find(get_class($group1), $groupId));
  48. }
  49. $this->_em->flush();
  50. $this->_em->commit();
  51. } catch(\Exception $e) {
  52. $this->_em->rollback();
  53. }
  54. }
  55. }