3 namespace Doctrine\Tests\ORM\Functional\Ticket;
5 require_once __DIR__ . '/../../../TestInit.php';
7 class DDC881Test extends \Doctrine\Tests\OrmFunctionalTestCase
10 protected function setUp()
15 $this->_schemaTool->createSchema(array(
16 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881User'),
17 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881Phonenumber'),
18 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881Phonecall'),
20 } catch (\Exception $e) {
29 public function testIssue()
31 /* Create two test users: albert and alfons */
32 $albert = new DDC881User;
33 $albert->setName("albert");
34 $this->_em->persist($albert);
36 $alfons = new DDC881User;
37 $alfons->setName("alfons");
38 $this->_em->persist($alfons);
42 /* Assign two phone numbers to each user */
43 $phoneAlbert1 = new DDC881PhoneNumber();
44 $phoneAlbert1->setUser($albert);
45 $phoneAlbert1->setId(1);
46 $phoneAlbert1->setPhoneNumber("albert home: 012345");
47 $this->_em->persist($phoneAlbert1);
49 $phoneAlbert2 = new DDC881PhoneNumber();
50 $phoneAlbert2->setUser($albert);
51 $phoneAlbert2->setId(2);
52 $phoneAlbert2->setPhoneNumber("albert mobile: 67890");
53 $this->_em->persist($phoneAlbert2);
55 $phoneAlfons1 = new DDC881PhoneNumber();
56 $phoneAlfons1->setId(1);
57 $phoneAlfons1->setUser($alfons);
58 $phoneAlfons1->setPhoneNumber("alfons home: 012345");
59 $this->_em->persist($phoneAlfons1);
61 $phoneAlfons2 = new DDC881PhoneNumber();
62 $phoneAlfons2->setId(2);
63 $phoneAlfons2->setUser($alfons);
64 $phoneAlfons2->setPhoneNumber("alfons mobile: 67890");
65 $this->_em->persist($phoneAlfons2);
67 /* We call alfons and albert once on their mobile numbers */
68 $call1 = new DDC881PhoneCall();
69 $call1->setPhoneNumber($phoneAlfons2);
70 $this->_em->persist($call1);
72 $call2 = new DDC881PhoneCall();
73 $call2->setPhoneNumber($phoneAlbert2);
74 $this->_em->persist($call2);
79 // fetch-join that foreign-key/primary-key entity association
80 $dql = "SELECT c, p FROM " . __NAMESPACE__ . "\DDC881PhoneCall c JOIN c.phonenumber p";
81 $calls = $this->_em->createQuery($dql)->getResult();
83 $this->assertEquals(2, count($calls));
84 $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $calls[0]->getPhoneNumber());
85 $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $calls[1]->getPhoneNumber());
87 $dql = "SELECT p, c FROM " . __NAMESPACE__ . "\DDC881PhoneNumber p JOIN p.calls c";
88 $numbers = $this->_em->createQuery($dql)->getResult();
90 $this->assertEquals(2, count($numbers));
91 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $numbers[0]->getCalls());
92 $this->assertTrue($numbers[0]->getCalls()->isInitialized());
105 * @Column(type="integer")
106 * @GeneratedValue(strategy="AUTO")
110 * @Column(type="string")
114 * @OneToMany(targetEntity="DDC881PhoneNumber",mappedBy="id")
116 private $phoneNumbers;
118 public function getName()
123 public function setName($name)
132 class DDC881PhoneNumber
137 * @Column(type="integer")
142 * @ManyToOne(targetEntity="DDC881User",cascade={"all"})
146 * @Column(type="string")
148 private $phonenumber;
151 * @OneToMany(targetEntity="DDC881PhoneCall", mappedBy="phonenumber")
155 public function __construct()
157 $this->calls = new \Doctrine\Common\Collections\ArrayCollection();
160 public function setId($id)
165 public function setUser(DDC881User $user)
170 public function setPhoneNumber($phoneNumber)
172 $this->phonenumber = $phoneNumber;
175 public function getCalls()
184 class DDC881PhoneCall
189 * @Column(type="integer")
190 * @GeneratedValue(strategy="AUTO")
194 * @ManyToOne(targetEntity="DDC881PhoneNumber", inversedBy="calls", cascade={"all"})
196 * @JoinColumn(name="phonenumber_id", referencedColumnName="id"),
197 * @JoinColumn(name="user_id", referencedColumnName="user_id")
200 private $phonenumber;
202 * @Column(type="string",nullable=true)
206 public function setPhoneNumber(DDC881PhoneNumber $phoneNumber)
208 $this->phonenumber = $phoneNumber;
211 public function getPhoneNumber()
213 return $this->phonenumber;