OneToManyOrphanRemovalTest.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\CMS\CmsUser,
  4. Doctrine\Tests\Models\CMS\CmsAddress,
  5. Doctrine\Tests\Models\CMS\CmsPhonenumber;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a bidirectional one-to-many association mapping with orphan removal.
  9. */
  10. class OneToManyOrphanRemovalTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. protected $userId;
  13. protected function setUp()
  14. {
  15. $this->useModelSet('cms');
  16. parent::setUp();
  17. $user = new CmsUser;
  18. $user->status = 'dev';
  19. $user->username = 'romanb';
  20. $user->name = 'Roman B.';
  21. $phone = new CmsPhonenumber;
  22. $phone->phonenumber = '123456';
  23. $user->addPhonenumber($phone);
  24. $this->_em->persist($user);
  25. $this->_em->flush();
  26. $this->userId = $user->getId();
  27. $this->_em->clear();
  28. }
  29. public function testOrphanRemoval()
  30. {
  31. $userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
  32. $this->_em->remove($userProxy);
  33. $this->_em->flush();
  34. $this->_em->clear();
  35. $query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
  36. $result = $query->getResult();
  37. $this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager');
  38. $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
  39. $result = $query->getResult();
  40. $this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
  41. }
  42. /**
  43. * @group DDC-1496
  44. */
  45. public function testOrphanRemovalUnitializedCollection()
  46. {
  47. $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
  48. $user->phonenumbers->clear();
  49. $this->_em->flush();
  50. $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
  51. $result = $query->getResult();
  52. $this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
  53. }
  54. }