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:

1<?php /** @Document */ class Article { /** @Id */ private $id; /** @ReferenceOne(targetDocument="User") */ private $author; }
2
3
4
5
6
7
8
9
10

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:

1{ "_id": "1234", "doctrine_metadata": { "associations": { "author": "54321" } }, "title": "An article", "type": "Article" } { "_id": "54321", "name": "Benjamin", "type": "User" }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

ReferenceMany

You can have a reference between a document and a set of related documents in the following way

1<?php /** * @Document */ class Article { /** @Id */ private $id; /** @ReferenceMany(targetDocument="Comment") */ private $comments; }
2
3
4
5
6
7
8
9
10
11
12

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:

1<?php /** @Document */ class User { /** @Id */ private $id; /** * @ReferenceMany(targetDocument="Article", mappedBy="author") */ private $articles; }
2
3
4
5
6
7
8
9
10
11
12

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:

1{ "_id": "54321", "name": "Benjamin", "type": "User" }
2
3
4
5

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:

1<?php /** @Document */ class User { /** @Id */ private $id; /** @EmbedOne */ private $address; }
2
3
4
5
6
7
8
9
10

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:

1{ "_id": "1234", "address": { "zipcode": "12345", "city": "Berlin" } }
2
3
4
5
6
7
8

EmbedMany

You can embed an array of classes into a document.

1<?php /** @Document */ class User { /** @Id */ private $id; /** @EmbedMany */ private $phonenumbers; }
2
3
4
5
6
7
8
9
10

In CouchDB documents with this relationship will look like:

1{ "_id": "1234", "phonenumbers": [ {"number": "+1234567890"}, {"number": "+1234567891"} ] }
2
3
4
5
6
7
8