Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Referencing Embedded document #500

babarinde opened this Issue · 5 comments

5 participants


Hi Guys,
First Thanks for the great job.
I need to ask,
Say I have a document A and a collection of embedded document B in A

/** @Document */
class A {
    /** @Id */
    protected $id;

    /** @EmbedMany(targetDocument="B") */
    protected $b;

/** @EmbeddedDocument */
class B {
    /** @Id */
    protected $id;

    /** @String */
    protected $string;

Is it possible For a new document C to reference an embedded document B from a document of A?


It is not supported right now, but you can simply reference A from C and have a getter on C to go directly to B.


Thanks will look into your suggestion.


References to Embedded Documents would be a great feature.


I found this issue while researching a related behavior. Consider the following case:

 * @ODM\Document(collection="a")
class A 
    /** @ODM\EmbedOne(targetDocument="E", name="e") */
    protected $e;

 * @ODM\EmbeddedDocument
class E
     * @ODM\Id
    protected $id;

 * @ODM\Document(collection="b")
class B 
     * @ODM\ReferenceOne(
     *  targetDocument="E",
     *  name="embedded_reference"
     * )
    protected $embeddedReference;


$a = new A();
$a->update(['e' => new E()]);

$a now contains an embedded document $e that will auto-generate an object id when saved.


Accessing the $id property returns an expected reference id.


$b = new B();
$originalId = $a->get('e')->get('id');
$b->update(['embeddedReference' => $a->get('e')]);

At this point, the id saved in the 'b' collection in the mongoDb is different than the id of the embedded document we're trying to reference. This can be verified by doing a db.b.find() inside mongo. It appears that doctrine creates a new B document during the flush and saves the ID in the reference property. The newly created embedded document doesn't end up attached to a document, and is subsequently destroyed.

$newId = $b->get('embeddedReference')->get('id');

will generate a 'document with identifier "x" could not be found error


Hi @julesbro ,
Your E class is an defined as an embedded document but you are trying to create a reference to it in B, which is not possible.
As of now (i think), the ReferenceOne annotation would only work for targets that are documents and not embedded documents.
As a feature for referencing embedded docs does not exist now
I hope this was helpful, cheers

@jmikola jmikola added this to the 1.x milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.