Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmbedMany #54

Open
joshystuart opened this issue May 25, 2014 · 4 comments
Open

EmbedMany #54

joshystuart opened this issue May 25, 2014 · 4 comments
Labels

Comments

@joshystuart
Copy link
Member

Currently EmbedMany fails using a discriminatorMap here: https://github.com/zoopcommerce/shard/blob/master/lib/Zoop/Shard/ODMCore/PreLoadSubscriber.php#L55

See doctrine docs for allowed attributes: http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/annotations-reference.html#embedmany

@superdweebie
Copy link
Contributor

The preLoadSubscriber is there so that access control gets honoured for embedded documents. Here's what needs to be done. When using a discriminator map, target document isn't set, which is why the code is failing. Need to add a new if to first check for a discriminiator map - if it exisists, then use it, if not, then use the target document. Something like this should work:

if (isset($mapping['discriminatorField'])) {
    $targetMetadata = $documentManager->getClassMetadata($mapping['discriminatorMap'][$eventArgs->getData[$mapping['discriminatorField']]]);
} else {
    $targetMetadata = $documentManager->getClassMetadata($mapping['targetDocument']);
}

:)

@joshystuart
Copy link
Member Author

Thanks for the advice 👍

I wasn't entirely sure how to approach the $mapping['discriminatorMap'][$eventArgs->getData[$mapping['discriminatorField']]] section.

@superdweebie
Copy link
Contributor

$eventArgs->getData contains the raw data from the database, before it has been hydrated into a document.

Actually, I just realised I made a mistake. Try this:

if (isset($mapping['discriminatorField'])) {
    $targetMetadata = $documentManager->getClassMetadata(
        $mapping['discriminatorMap'][$eventArgs->getData[$field][$mapping['discriminatorField']]]
    );
} else {
    $targetMetadata = $documentManager->getClassMetadata($mapping['targetDocument']);
}

Or the long version of the important bit:

$unhydratedEmbeddedDoc = $eventArgs->getData[$field];
$discriminatorFieldValue = $unhydratedEmbeddedDoc[$mapping['discriminatorField']];
$embeddedClassName = $mapping['discriminatorMap'][$discriminatorFieldValue];
$targetMetadata = $documentManager->getClassMetadata($embeddedClassName);

@joshystuart
Copy link
Member Author

Unfortunately it's a little more complicated than that.

If you have something like:

/**
     * @ODM\EmbedMany(
     *   discriminatorMap={
     *     "Artist"="Artist",
     *     "SongWriter"="SongWriter"
     *   }
     * ) 
     */
    protected $songWriters = [];

I think we need to loop through each item within the field and check against it's discriminatorFieldValue.

I'm going to submit a PR soon. It's a little messy so I'd like to get your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants