Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Referencing Embedded document #500

Open
babarinde opened this Issue · 5 comments

5 participants

@babarinde

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?
Thanks

@jwage
Owner

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.

@babarinde

@jwage
Thanks will look into your suggestion.

@karser

References to Embedded Documents would be a great feature.

@julesbro

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;
}

subsequently:

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

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

$a->get('id');

Accessing the $id property returns an expected reference id.

Now:

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

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.
Attempting:

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

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

@babarinde

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.