1

Take the documentation example:

/** @Document */
class BlogPost
{
    // ...

    /** @ReferenceOne(targetDocument="User", inversedBy="posts") */
    private $user;
}

/** @Document */
class User
{
    // ...

    /** @ReferenceMany(targetDocument="BlogPost", mappedBy="user") */
    private $posts;
}

There is any way that i can query for the User entity and retrieve all his posts?

i mean i can use the example query:

db.BlogPost.find({ 'user.$id' : user.id })

But what if i want to do:

db.User.find({ 'id' : id })

And access user posts through User property $posts

Can this be archived?

EDIT

When i perform the query we can found the logs:

[2016-08-15 17:13:45] doctrine.DEBUG: MongoDB query: {"find":true,"query":{"_id":{}},"fields":[],"db":"base","collection":"User"} [] []
[2016-08-15 17:13:45] doctrine.DEBUG: MongoDB query: {"limit":true,"limitNum":1,"query":{"_id":{}},"fields":[]} [] []
[2016-08-15 17:13:45] doctrine.DEBUG: MongoDB query: {"limit":true,"limitNum":null,"query":{"_id":{}},"fields":[]} [] []
bitgandtter
  • 2,179
  • 6
  • 31
  • 60

1 Answers1

0

With Doctrine ODM :

$dm = $this->get('doctrine_mongodb')->getManager();

$user = $dm->getRepository('AppBundle:User')->findOneById($id);
$posts = $user->getPosts();
Alsatian
  • 3,086
  • 4
  • 25
  • 40
  • i already try that and getPosts return an empty collection, anything else that i need to review or check that may causing this to happens? – bitgandtter Aug 15 '16 at 12:58
  • Did you fill you DB with doctrine ? – Alsatian Aug 15 '16 at 13:15
  • Read this : http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/reference-mapping.html#storing-references – Alsatian Aug 15 '16 at 13:18
  • yes, the workflow im working on, specifically a test scenario; involves first creating some posts for the user then try to retrieve those posts from it, but its returning empty. – bitgandtter Aug 15 '16 at 14:06
  • Check the MongoDB queries made by Doctrine in the Symfony Profiler. And try to use them directly in MongoDB shell. If it don't work, your mapping is not good. (You can add these resulting query to your question) – Alsatian Aug 15 '16 at 14:18
  • i just add the logs from symfony and doctrine refereeing the query – bitgandtter Aug 15 '16 at 23:26