Skip to content

Inline field denormalization for Reference* mapping #613

Open
@j

Description

@j

Think of the following two documents:

<?php

/** @Document() */
class Survey {
    /** @Id() */
    public $id;

    /** @ReferenceMany(targetDocument="Question", embedFields={"question"}) */
    public $questions;
}

/** @Document() */
class Question {
    /** @Id() */
    public $id;

    /** @String() */
    public $question;
}

The mongo stored documents for surveys would look like:

{
    "_id": "...",
    "questions": [
        { "$ref": "...", "$id": "...", "$db": "...", "question": "Is ODM awesome?" },
        { "$ref": "...", "$id": "...", "$db": "...", "question": "Isn't this such a great feature?!" },
    ]
}

So that if you do the following, no extra finds will be triggered unless the field wasn't embedded in the referenced document data:

<?php

$questions = [];
foreach ($surveyRepository->find("...")->getQuestions() as $question) {
    $questions[$question->getId()] = $question->getQuestion();
}

Updates to the question may cascade unless configured to not do so, or the other way around. In most of my cases, a cascade can occur.. but if someone happens to use this feature on an active database with many records, then shit can hit the fan!

It'd be easy to create a document and manage it as if it were it's own document and just associate that to one thing and be embedded. I tend to use it this way in many cases... thus would limit many finds.

Another thing that can also solve this problem is to introduce caching, but that's another topic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions