Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 212 lines (157 sloc) 6.841 kb
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
1 .. _inheritance_mapping:
2
4233232 @jwage Fixing titles.
jwage authored
3 Inheritance Mapping
4 ===================
5
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
6 Doctrine currently offers two supported methods of inheritance:
7 :ref:`single collection <single_collection_inheritance>` and
8 :ref:`collection per class <collection_per_class_inheritance>` inheritance.
2311055 @jwage Convert to rst.
jwage authored
9
10 Mapped Superclasses
11 -------------------
12
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
13 A mapped superclass is an abstract or concrete class that provides mapping
14 information for its subclasses, but is not itself a document. Typically, the
15 purpose of such a mapped superclass is to define state and mapping information
16 that is common to multiple document classes.
2311055 @jwage Convert to rst.
jwage authored
17
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
18 Just like non-mapped classes, mapped superclasses may appear in the middle of
19 an otherwise mapped inheritance hierarchy (through
20 :ref:`single collection <single_collection_inheritance>` or
21 :ref:`collection per class <collection_per_class_inheritance>`) inheritance.
2311055 @jwage Convert to rst.
jwage authored
22
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
23 .. note::
24
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
25 A mapped superclass cannot be a document and is not queryable.
2311055 @jwage Convert to rst.
jwage authored
26
27 Example:
28
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
29 .. configuration-block::
2311055 @jwage Convert to rst.
jwage authored
30
c5b48cb @jwage Adding new theme.
jwage authored
31 .. code-block:: php
d9b9e9b @jwage Updating documentation for latest code in master and change to rst.
jwage authored
32
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
33 <?php
d9b9e9b @jwage Updating documentation for latest code in master and change to rst.
jwage authored
34
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
35 namespace Documents;
d9b9e9b @jwage Updating documentation for latest code in master and change to rst.
jwage authored
36
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
37 /** @MappedSuperclass */
38 abstract class BaseDocument
39 {
40 }
41
42 .. code-block:: xml
43
44 <?xml version="1.0" encoding="UTF-8"?>
45 <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
46 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47 xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
48 http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
49 <mapped-superclass name="Documents\BaseDocument">
50 </mapped-superclass>
51 </doctrine-mongo-mapping>
52
53 .. code-block:: yaml
54
55 Documents\BaseDocument:
56 type: mappedSuperclass
2311055 @jwage Convert to rst.
jwage authored
57
84c4234 @jmikola Document referencing priming changes
jmikola authored
58 .. _single_collection_inheritance:
59
2311055 @jwage Convert to rst.
jwage authored
60 Single Collection Inheritance
61 -----------------------------
62
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
63 In single collection inheritance, each document is stored in a single collection
64 and a discriminator field is used to distinguish one document type from another.
2311055 @jwage Convert to rst.
jwage authored
65
66 Simple example:
67
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
68 .. configuration-block::
2311055 @jwage Convert to rst.
jwage authored
69
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
70 .. code-block:: php
71
72 <?php
d9b9e9b @jwage Updating documentation for latest code in master and change to rst.
jwage authored
73
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
74 namespace Documents;
2311055 @jwage Convert to rst.
jwage authored
75
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
76 /**
77 * @Document
78 * @InheritanceType("SINGLE_COLLECTION")
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
79 * @DiscriminatorField("type")
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
80 * @DiscriminatorMap({"person"="Person", "employee"="Employee"})
81 */
82 class Person
83 {
84 // ...
85 }
2311055 @jwage Convert to rst.
jwage authored
86
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
87 /**
88 * @Document
89 */
90 class Employee extends Person
91 {
92 // ...
93 }
94
95 .. code-block:: xml
96
97 <?xml version="1.0" encoding="UTF-8"?>
98 <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
99 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
100 xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
101 http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
102 <document name="Documents\Person" inheritance-type="SINGLE_COLLECTION">
ebb7d5f @jmikola Discriminator fields are not mapped; ClassMetadata property change
jmikola authored
103 <discriminator-field name="type" />
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
104 <discriminator-map>
105 <discriminator-mapping value="person" class="Person" />
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
106 <discriminator-mapping value="employee" class="Employee" />
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
107 </discriminator-map>
108 </document>
109 </doctrine-mongo-mapping>
110
111 <?xml version="1.0" encoding="UTF-8"?>
112 <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
113 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
114 xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
115 http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
116 <document name="Documents\Employee">
117 </document>
118 </doctrine-mongo-mapping>
119
120 .. code-block:: yaml
121
122 Documents\Person:
123 type: document
124 inheritanceType: SINGLE_COLLECTION
de5c920 @ZCJ Update inheritance-mapping.rst
ZCJ authored
125 discriminatorField: type
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
126 discriminatorMap:
127 person: Person
128 employee: Employee
2311055 @jwage Convert to rst.
jwage authored
129
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
130 The discriminator value allows Doctrine to infer the class name to instantiate
131 when hydrating a document. If a discriminator map is used, the discriminator
132 value will be used to look up the class name in the map.
133
134 Now, if we query for a Person and its discriminator value is ``employee``, we
135 would get an Employee instance back:
2311055 @jwage Convert to rst.
jwage authored
136
32ca039 @jwage Fixing doc blocks.
jwage authored
137 .. code-block:: php
2311055 @jwage Convert to rst.
jwage authored
138
139 <?php
d9b9e9b @jwage Updating documentation for latest code in master and change to rst.
jwage authored
140
f8d392d Fixed typo
DenisGorbachev authored
141 $employee = new Employee();
2311055 @jwage Convert to rst.
jwage authored
142 // ...
143 $dm->persist($employee);
144 $dm->flush();
145
146 $employee = $dm->find('Person', $employee->getId()); // instanceof Employee
147
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
148 Even though we queried for a Person, Doctrine will know to return an Employee
149 instance because of the discriminator map!
150
151 .. _collection_per_class_inheritance:
2311055 @jwage Convert to rst.
jwage authored
152
153 Collection Per Class Inheritance
154 --------------------------------
155
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
156 With collection per class inheritance, each document is stored in its own
157 collection and contains all inherited fields:
2311055 @jwage Convert to rst.
jwage authored
158
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
159 .. configuration-block::
2311055 @jwage Convert to rst.
jwage authored
160
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
161 .. code-block:: php
162
163 <?php
d9b9e9b @jwage Updating documentation for latest code in master and change to rst.
jwage authored
164
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
165 namespace Documents;
2311055 @jwage Convert to rst.
jwage authored
166
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
167 /**
168 * @Document
169 * @InheritanceType("COLLECTION_PER_CLASS")
170 */
171 class Person
172 {
173 // ...
174 }
2311055 @jwage Convert to rst.
jwage authored
175
0daea8d @jwage Updating all examples to show annotations, xml and yaml
jwage authored
176 /**
177 * @Document
178 */
179 class Employee extends Person
180 {
181 // ...
182 }
183
184 .. code-block:: xml
185
186 <?xml version="1.0" encoding="UTF-8"?>
187 <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
188 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
189 xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
190 http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
191 <document name="Documents\Person" inheritance-type="COLLECTION_PER_CLASS">
192 </document>
193 </doctrine-mongo-mapping>
194
195 <?xml version="1.0" encoding="UTF-8"?>
196 <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
197 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
198 xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
199 http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
200 <document name="Documents\Employee">
201 </document>
202 </doctrine-mongo-mapping>
203
204 .. code-block:: yaml
205
206 Documents\Person:
207 type: document
208 inheritanceType: COLLECTION_PER_CLASS
2311055 @jwage Convert to rst.
jwage authored
209
c738ba1 @jmikola Revise documentation for discriminators and inheritance types
jmikola authored
210 A discriminator is not needed with this type of inheritance since the data is
211 separated in different collections.
Something went wrong with that request. Please try again.