This project is not being actively maintained. If you are interested in helping to maintain this project, take a look at the open issues on GitHub and submit pull requests. |
Association Mapping
Graph Traversal and Identity Map
Doctrine CouchDB offers you a fluent experience with your CouchDB documents. All relationships are fully traversable through lazy loading. You essentially need to query only one document explicitly and can then access all the others that are related by traversing the references in the objects.
A document manager contains an identity map, it will never return you different instances of PHP objects for the document with the same ID.
Different Association Mappings
Doctrine CouchDB offers several different mechanisms to manage relations between documents:
- Reference one document through its document-id
- Reference many documents by saving all the document-ids in an array
- Finding all documents that have a reference many relationship to the current document (inverse reference many/one)
- Embedding a single document
- Embedding an array of documents
ReferenceOne
You can have a reference between two documents by a reference one relationship in the following way:
This mapping definition would save the author id or null in the CouchDB document but let you work with a Author object in PHP.
In CouchDB two documents with this relationship will look like:
ReferenceMany
You can have a reference between a document and a set of related documents in the following way
This mapping definition will save an array of comment ids in every article document and
will present you with a Doctrine\Common\Collections\Collection
containing Comment instances
in PHP.
In CouchDB documents with this relationship will look like:
1 {
"_id": "1234",
"comments": ["55555", "44444"],
"doctrine_metadata":
{
"associations": ["comments"]
},
"title": "An article",
"type": "Article"
}
{
"_id": "55555",
"text": "Thank you!",
"type": "Comment"
}
{
"_id": "44444",
"text": "Very informative!",
"type": "Comment"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Inverse ReferenceMany
You can define the inverse side of a reference one or reference many association, which will use a view to access which owning side documents point to the current document by holding a reference to their id:
See the difference between the previous reference many definition by using the mappedBy attribute. This specifies which association on the target document contains the id reference.
In CouchDB documents with this relationship will look like:
A view is used to lookup the related articles. The view emits type, all associations and their ids.
EmbedOne
You can embed a class into a document. Both will be saved in the same CouchDB document:
The embed one mapping definition does not necessarily need a "targetDocument" attribute, it can detect and save this automatically.
In CouchDB documents with this relationship will look like:
EmbedMany
You can embed an array of classes into a document.
In CouchDB documents with this relationship will look like: