Skip to content

Commit 7613b91

Browse files
committed
Inheritance support
1 parent 7a18fbc commit 7613b91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1227
-515
lines changed

README.markdown

+67-11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,62 @@ Now you are ready to start defining PHP 5.3 classes and persisting them to Mongo
8181
public $name;
8282
}
8383

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+
84140
## Persisting Documents
85141

86142
Create a new instance, set some of the properties and persist it:
@@ -133,25 +189,25 @@ method:
133189

134190
$user = $dm->findByID('User', 'the_string_id');
135191

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:
138194

139-
$dm->loadDocumentAssociations($user);
195+
$dm->loadDocumentReferences($user);
140196

141197
Now you can access the ->account property and get an Account instance:
142198

143199
echo $user->account->name; // Test Account
144200

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)
146202
method:
147203

148-
$dm->loadDocumentAssociation($user, 'account');
204+
$dm->loadDocumentReference($user, 'account');
149205

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:
152208

153209
$query = $dm->createQuery('User')
154-
->loadAssociation('account');
210+
->loadReference('account');
155211

156212
$users = $query->execute();
157213
foreach ($users as $user) {
@@ -264,7 +320,7 @@ Now you can later query for the Image and render it:
264320
header('Content-type: image/png;');
265321
echo $image->getFile()->getBytes();
266322

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.
268324
Imagine you had a Profile document and you wanted every Profile to have a profile
269325
image:
270326

@@ -325,11 +381,11 @@ Now you can create a new Profile and give it an Image:
325381
$dm->persist($profile);
326382
$dm->flush();
327383

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
329385
you can use:
330386

331387
$profile = $dm->createQuery('Profile')
332-
->loadAssociation('image')
388+
->loadReference('image')
333389
->where('name', 'Jonathan H. Wage')
334390
->getSingleResult();
335391

example/Documents/Admin.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
*/
8+
class Admin extends Moderator
9+
{
10+
/** @Field */
11+
private $adminField = 'test';
12+
}

example/Documents/BaseDocument.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
* @InheritanceType("COLLECTION_PER_CLASS")
8+
* @DiscriminatorMap={"comment"="Documents\Comment", "my_comment"="Documents\MyComment"})
9+
*/
10+
abstract class BaseDocument
11+
{
12+
/** @Id */
13+
protected $id;
14+
15+
/** @Field */
16+
protected $name;
17+
18+
public function getId()
19+
{
20+
return $this->id;
21+
}
22+
23+
public function setName($name)
24+
{
25+
$this->name = $name;
26+
}
27+
28+
public function getName()
29+
{
30+
return $this->name;
31+
}
32+
}

example/Documents/BlogPost.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
*/
8+
class BlogPost extends Page
9+
{
10+
/** @Teaser */
11+
private $teaser;
12+
13+
/** @Field */
14+
private $body;
15+
16+
public function setTeaser($teaser)
17+
{
18+
$this->teaser = $teaser;
19+
}
20+
21+
public function getTeaser()
22+
{
23+
return $this->teaser;
24+
}
25+
26+
public function setBody($body)
27+
{
28+
$this->body = $body;
29+
}
30+
31+
public function getBody()
32+
{
33+
return $this->body;
34+
}
35+
}

example/Documents/Comment.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
*/
8+
class Comment extends BaseDocument
9+
{
10+
/** @Field */
11+
private $comment;
12+
13+
public function setComment($comment)
14+
{
15+
$this->comment = $comment;
16+
}
17+
18+
public function getComment()
19+
{
20+
return $this->comment;
21+
}
22+
}

example/Documents/Moderator.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
*/
8+
class Moderator extends User
9+
{
10+
11+
}

example/Documents/MyComment.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
*/
8+
class MyComment extends Comment
9+
{
10+
/** @Field */
11+
private $test;
12+
13+
public function setTest($test)
14+
{
15+
$this->test = $test;
16+
}
17+
18+
public function getTest()
19+
{
20+
return $this->test;
21+
}
22+
}

example/Documents/Node.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
* @InheritanceType("SINGLE_COLLECTION")
8+
* @DiscriminatorField(fieldName="type")
9+
* @DiscriminatorMap({"page"="Documents\Page", "blog_post"="Documents\BlogPost"})
10+
*/
11+
abstract class Node
12+
{
13+
/** @Id */
14+
protected $id;
15+
16+
public function getId()
17+
{
18+
return $this->id;
19+
}
20+
}

example/Documents/Page.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
/**
6+
* @Document
7+
*/
8+
class Page extends Node
9+
{
10+
/** @Field */
11+
protected $title;
12+
13+
public function setTitle($title)
14+
{
15+
$this->title = $title;
16+
}
17+
18+
public function getTitle()
19+
{
20+
return $this->title;
21+
}
22+
}

example/Documents/User.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,32 @@
55
/** @Document(indexes={
66
* @Index(keys={"username"="desc"}, options={"unique"=true})
77
* })
8+
* @InheritanceType("SINGLE_COLLECTION")
9+
* @DiscriminatorField(fieldName="type")
10+
* @DiscriminatorMap({"moderator"="Documents\Moderator", "admin"="Documents\Admin"})
811
*/
912
class User
1013
{
1114
/** @Id */
12-
private $id;
15+
protected $id;
1316

1417
/** @Field */
15-
private $username;
18+
protected $username;
1619

1720
/** @Field */
18-
private $password;
21+
protected $password;
1922

2023
/** @EmbedMany(targetDocument="Documents\Phonenumber") */
21-
private $phonenumbers = array();
24+
protected $phonenumbers = array();
2225

2326
/** @EmbedMany(targetDocument="Documents\Address") */
24-
private $addresses = array();
27+
protected $addresses = array();
2528

2629
/** @ReferenceOne(targetDocument="Documents\Profile", cascadeDelete="true") */
27-
private $profile;
30+
protected $profile;
2831

2932
/** @ReferenceOne(targetDocument="Documents\Account", cascadeDelete="true") */
30-
private $account;
33+
protected $account;
3134

3235
public function getId()
3336
{

0 commit comments

Comments
 (0)