Skip to content

Commit 3732163

Browse files
committed
Add test for doctrine#267
1 parent bc62751 commit 3732163

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace Doctrine\ODM\MongoDB\Tests;
4+
5+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
6+
7+
class GH267Test extends BaseTest
8+
{
9+
public function testNestedReferences()
10+
{
11+
// Users
12+
$user1 = new User('Tom Petty');
13+
$user2 = new User('Grateful Dead');
14+
$user3 = new User('Neil Young');
15+
16+
// Company
17+
$company = new BuyerCompany();
18+
19+
$user1->setCompany($company);
20+
$user2->setCompany($company);
21+
$user3->setCompany($company);
22+
23+
$this->dm->persist($company);
24+
$this->dm->flush();
25+
26+
$this->dm->persist($user1);
27+
$this->dm->persist($user2);
28+
$this->dm->persist($user3);
29+
$this->dm->flush();
30+
31+
// Get ids for use later
32+
$user1Id = $user1->getId();
33+
$companyId = $company->getId();
34+
35+
// Clear out DM and read from DB afresh
36+
$this->dm->clear();
37+
38+
$qb = $this->dm->createQueryBuilder(__NAMESPACE__ . '\User')
39+
->field('_id')->equals($user1Id);
40+
41+
$query = $qb->getQuery();
42+
$dbUser = $query->execute()->getNext();
43+
44+
// Assert user name
45+
$this->assertEquals('Tom Petty', $dbUser->getName());
46+
47+
// Assert company id
48+
$this->assertEquals($companyId, $dbUser->getCompany()->getId());
49+
50+
// Assert number of users
51+
$this->assertEquals(3, $dbUser->getCompany()->getUsers()->count(true));
52+
}
53+
}
54+
55+
/**
56+
* @ODM\Document(collection="users")
57+
*/
58+
class User
59+
{
60+
/** @ODM\Id */
61+
protected $id;
62+
63+
/** @ODM\String */
64+
protected $name;
65+
66+
/**
67+
* @ODM\ReferenceOne(name="company", targetDocument="Company", discriminatorMap={"seller"="SellerCompany", "buyer"="BuyerCompany"}, inversedBy="users")
68+
*/
69+
protected $company;
70+
71+
public function __construct($name)
72+
{
73+
$this->name = $name;
74+
}
75+
76+
public function setId($id)
77+
{
78+
$this->id = $id;
79+
}
80+
81+
public function getId()
82+
{
83+
return $this->id;
84+
}
85+
86+
public function setName($name)
87+
{
88+
$this->name = $name;
89+
}
90+
91+
public function getName()
92+
{
93+
return $this->name;
94+
}
95+
96+
public function setCompany($company)
97+
{
98+
$this->company = $company;
99+
}
100+
101+
public function getCompany()
102+
{
103+
return $this->company;
104+
}
105+
}
106+
107+
/**
108+
* @ODM\Document(collection="companies")
109+
* @ODM\InheritanceType("SINGLE_COLLECTION")
110+
* @ODM\DiscriminatorField(fieldName="type")
111+
* @ODM\DiscriminatorMap({"seller"="SellerCompany", "buyer"="BuyerCompany"})
112+
*/
113+
class Company
114+
{
115+
/** @ODM\Id */
116+
protected $id;
117+
118+
/**
119+
* @ODM\ReferenceMany(targetDocument="User", mappedBy="company")
120+
*/
121+
protected $users;
122+
123+
public function setId($id)
124+
{
125+
$this->id = $id;
126+
}
127+
128+
public function getId()
129+
{
130+
return $this->id;
131+
}
132+
133+
public function setUsers($users)
134+
{
135+
$this->users = $users;
136+
}
137+
138+
public function getUsers()
139+
{
140+
return $this->users;
141+
}
142+
}
143+
144+
/**
145+
* @ODM\Document(collection="companies")
146+
*/
147+
class BuyerCompany extends Company
148+
{
149+
150+
}
151+
152+
/**
153+
* @ODM\Document(collection="companies")
154+
*/
155+
class SellerCompany extends Company
156+
{
157+
158+
}

0 commit comments

Comments
 (0)