@@ -81,6 +81,62 @@ Now you are ready to start defining PHP 5.3 classes and persisting them to Mongo
81
81
public $name;
82
82
}
83
83
84
+ ## Inheritance Mapping
85
+
86
+ If you want to take advantage of inheritance you will need to specify some
87
+ mapping information for your documents:
88
+
89
+ ### Single Collection Inheritance
90
+
91
+ Each Document is stored in a single collection where a discriminator field is
92
+ automatically populated to keep track of what classes created each document
93
+ in the database:
94
+
95
+ namespace Documents;
96
+
97
+ /**
98
+ * @Document
99
+ * @InheritanceType("SINGLE_COLLECTION")
100
+ * @DiscriminatorField(fieldName="type")
101
+ * @DiscriminatorMap({"person"="Person", "employee"="Employee"})
102
+ */
103
+ class Person
104
+ {
105
+ // ...
106
+ }
107
+
108
+ /**
109
+ * @Document
110
+ */
111
+ class Employee extends Person
112
+ {
113
+ // ...
114
+ }
115
+
116
+ ### Collection Per Class Inheritance
117
+
118
+ Each Document is stored in its own collection:
119
+
120
+ namespace Documents;
121
+
122
+ /**
123
+ * @Document
124
+ * @InheritanceType("COLLECTION_PER_CLASS")
125
+ * @DiscriminatorMap({"person"="Person", "employee"="Employee"})
126
+ */
127
+ class Person
128
+ {
129
+ // ...
130
+ }
131
+
132
+ /**
133
+ * @Document
134
+ */
135
+ class Employee extends Person
136
+ {
137
+ // ...
138
+ }
139
+
84
140
## Persisting Documents
85
141
86
142
Create a new instance, set some of the properties and persist it:
@@ -133,25 +189,25 @@ method:
133
189
134
190
$user = $dm->findByID('User', 'the_string_id');
135
191
136
- You may want to load the associations for an document, you can do this with the
137
- loadDocumentAssociations () method:
192
+ You may want to load the references for an document, you can do this with the
193
+ loadDocumentReferences () method:
138
194
139
- $dm->loadDocumentAssociations ($user);
195
+ $dm->loadDocumentReferences ($user);
140
196
141
197
Now you can access the ->account property and get an Account instance:
142
198
143
199
echo $user->account->name; // Test Account
144
200
145
- If you only want to load a specific association you can use the loadDocumentAssociation ($name)
201
+ If you only want to load a specific reference you can use the loadDocumentReference ($name)
146
202
method:
147
203
148
- $dm->loadDocumentAssociation ($user, 'account');
204
+ $dm->loadDocumentReference ($user, 'account');
149
205
150
- To automatically load the association during hydration you can specify the
151
- association to load on a query with the loadAssociation () method:
206
+ To automatically load the reference during hydration you can specify the
207
+ reference to load on a query with the loadReference () method:
152
208
153
209
$query = $dm->createQuery('User')
154
- ->loadAssociation ('account');
210
+ ->loadReference ('account');
155
211
156
212
$users = $query->execute();
157
213
foreach ($users as $user) {
@@ -264,7 +320,7 @@ Now you can later query for the Image and render it:
264
320
header('Content-type: image/png;');
265
321
echo $image->getFile()->getBytes();
266
322
267
- You can of course make associations to this Image document from another document.
323
+ You can of course make references to this Image document from another document.
268
324
Imagine you had a Profile document and you wanted every Profile to have a profile
269
325
image:
270
326
@@ -325,11 +381,11 @@ Now you can create a new Profile and give it an Image:
325
381
$dm->persist($profile);
326
382
$dm->flush();
327
383
328
- If you want to query for the Profile and load the Image association in a query
384
+ If you want to query for the Profile and load the Image reference in a query
329
385
you can use:
330
386
331
387
$profile = $dm->createQuery('Profile')
332
- ->loadAssociation ('image')
388
+ ->loadReference ('image')
333
389
->where('name', 'Jonathan H. Wage')
334
390
->getSingleResult();
335
391
0 commit comments