DDC211Test.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC211Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. protected function setUp()
  7. {
  8. parent::setUp();
  9. $this->_schemaTool->createSchema(array(
  10. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC211User'),
  11. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC211Group')
  12. ));
  13. }
  14. public function testIssue()
  15. {
  16. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  17. $user = new DDC211User;
  18. $user->setName('John Doe');
  19. $this->_em->persist($user);
  20. $this->_em->flush();
  21. $groupNames = array('group 1', 'group 2', 'group 3', 'group 4');
  22. foreach ($groupNames as $name) {
  23. $group = new DDC211Group;
  24. $group->setName($name);
  25. $this->_em->persist($group);
  26. $this->_em->flush();
  27. if (!$user->getGroups()->contains($group)) {
  28. $user->getGroups()->add($group);
  29. $group->getUsers()->add($user);
  30. $this->_em->flush();
  31. }
  32. }
  33. $this->assertEquals(4, $user->getGroups()->count());
  34. }
  35. }
  36. /**
  37. * @Entity
  38. * @Table(name="ddc211_users")
  39. */
  40. class DDC211User
  41. {
  42. /**
  43. * @Id
  44. * @Column(name="id", type="integer")
  45. * @GeneratedValue(strategy="AUTO")
  46. */
  47. protected $id;
  48. /**
  49. * @Column(name="name", type="string")
  50. */
  51. protected $name;
  52. /**
  53. * @ManyToMany(targetEntity="DDC211Group", inversedBy="users")
  54. * @JoinTable(name="user_groups",
  55. * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
  56. * inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
  57. * )
  58. */
  59. protected $groups;
  60. public function __construct() {
  61. $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
  62. }
  63. public function setName($name) { $this->name = $name; }
  64. public function getGroups() { return $this->groups; }
  65. }
  66. /**
  67. * @Entity
  68. * @Table(name="ddc211_groups")
  69. */
  70. class DDC211Group
  71. {
  72. /**
  73. * @Id
  74. * @Column(name="id", type="integer")
  75. * @GeneratedValue(strategy="AUTO")
  76. */
  77. protected $id;
  78. /**
  79. * @Column(name="name", type="string")
  80. */
  81. protected $name;
  82. /**
  83. * @ManyToMany(targetEntity="DDC211User", mappedBy="groups")
  84. */
  85. protected $users;
  86. public function __construct() {
  87. $this->users = new \Doctrine\Common\Collections\ArrayCollection();
  88. }
  89. public function setName($name) { $this->name = $name; }
  90. public function getUsers() { return $this->users; }
  91. }