DDC742Test.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. class DDC742Test extends \Doctrine\Tests\OrmFunctionalTestCase
  6. {
  7. private $userCm;
  8. private $commentCm;
  9. protected function setUp()
  10. {
  11. parent::setUp();
  12. if (\extension_loaded('memcache')) {
  13. $memcache = new \Memcache();
  14. $memcache->addServer('localhost');
  15. $memcache->flush();
  16. $cacheDriver = new \Doctrine\Common\Cache\MemcacheCache();
  17. $cacheDriver->setMemcache($memcache);
  18. $this->_em->getMetadataFactory()->setCacheDriver($cacheDriver);
  19. } else if (\extension_loaded('apc')) {
  20. $this->_em->getMetadataFactory()->setCacheDriver(new \Doctrine\Common\Cache\ApcCache());
  21. }
  22. try {
  23. $this->_schemaTool->createSchema(array(
  24. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC742User'),
  25. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC742Comment')
  26. ));
  27. } catch(\Exception $e) {
  28. }
  29. // make sure classes will be deserialized from caches
  30. $this->_em->getMetadataFactory()->setMetadataFor(__NAMESPACE__ . '\DDC742User', null);
  31. $this->_em->getMetadataFactory()->setMetadataFor(__NAMESPACE__ . '\DDC742Comment', null);
  32. }
  33. public function testIssue()
  34. {
  35. $user = new DDC742User();
  36. $user->title = "Foo";
  37. $user->favoriteComments = new ArrayCollection();
  38. $comment1 = new DDC742Comment();
  39. $comment1->content = "foo";
  40. $comment2 = new DDC742Comment();
  41. $comment2->content = "bar";
  42. $comment3 = new DDC742Comment();
  43. $comment3->content = "baz";
  44. $user->favoriteComments->add($comment1);
  45. $user->favoriteComments->add($comment2);
  46. $this->_em->persist($user);
  47. $this->_em->persist($comment1);
  48. $this->_em->persist($comment2);
  49. $this->_em->persist($comment3);
  50. $this->_em->flush();
  51. $this->_em->clear();
  52. $user = $this->_em->find(get_class($user), $user->id);
  53. $comment3 = $this->_em->find(get_class($comment3), $comment3->id);
  54. $user->favoriteComments->add($comment3);
  55. $this->_em->flush();
  56. }
  57. }
  58. /**
  59. * @Entity
  60. * @Table(name="users")
  61. */
  62. class DDC742User
  63. {
  64. /**
  65. * User Id
  66. *
  67. * @Id
  68. * @GeneratedValue(strategy="AUTO")
  69. * @Column(type="integer")
  70. * @var integer
  71. */
  72. public $id;
  73. /**
  74. * @Column(length=100, type="string")
  75. * @var string
  76. */
  77. public $title;
  78. /**
  79. * @ManyToMany(targetEntity="DDC742Comment", cascade={"persist"}, fetch="EAGER")
  80. * @JoinTable(
  81. * name="user_comments",
  82. * joinColumns={@JoinColumn(name="user_id",referencedColumnName="id")},
  83. * inverseJoinColumns={@JoinColumn(name="comment_id", referencedColumnName="id")}
  84. * )
  85. *
  86. * @var Doctrine\ORM\PersistentCollection
  87. */
  88. public $favoriteComments;
  89. }
  90. /**
  91. * @Entity
  92. * @Table(name="comments")
  93. */
  94. class DDC742Comment
  95. {
  96. /**
  97. * User Id
  98. *
  99. * @Id
  100. * @GeneratedValue(strategy="AUTO")
  101. * @Column(type="integer")
  102. * @var integer
  103. */
  104. public $id;
  105. /**
  106. * @Column(length=100, type="string")
  107. * @var string
  108. */
  109. public $content;
  110. }