1

Does someone know where I can find some informations about Mongodb QueryBuilder in Symfony2 please? Maybe some examples or some tutorials. I googled the question but didn't find something good enough.. More specifically I want to retrieve a field in a document based on the embedded document. Here is an example of document :

{
"name":"Foo",
"age":"25",
"gender":"Male",
"products":[
        {
            "name":"Apple",
            "price":"12.00",
            "date":"2015-12-02"
        },
        {
            "name":"Banana",
            "price":"9.00",
            "date":"201-11-31"
        },
]

}

And I want to retrieve the name "Foo" based on the date in products array. At the moment I use distinct() to list all fields in products.

4
  • doctrine-orm.readthedocs.org/projects/doctrine-mongodb-odm/en/… Commented Dec 2, 2015 at 9:17
  • Thanks Malcolm, I've been on that page, but I wanted something with more complex examples, if it exists of course :D Commented Dec 2, 2015 at 9:24
  • I would suggest you change your question on how to achieve some specific query, because right now, it is up for debate if this question should be closed or no :-/ Commented Dec 2, 2015 at 9:30
  • Alright, there you go ;) Commented Dec 2, 2015 at 9:41

1 Answer 1

1

Simple doctrine mongodb example for EmbedMany in symfony

Assume that a League may have one or many Teams so it is a Embed Many like in your case. Full example is above. If you want more info then I think first 5 posts are enough for you.

EXAMPLE USAGE

->findOneByProperty('name', 'Premiership')

REPO

use Doctrine\ODM\MongoDB\DocumentRepository;

class LeagueRepository extends DocumentRepository
{
    /**
     * @param string $field
     * @param string $data
     *
     * @return array|null|object
     */
    public function findOneByProperty($field, $data)
    {
        return
            $this->createQueryBuilder('League')
                ->field($field)->equals($data)
                ->getQuery()
                ->getSingleResult();
    }
}

DUMMY DATA

db.getCollection('league').find({})
/* 1 */
{
    "_id" : ObjectId("564fa07add576ebcf90041ac"),
    "name" : "Super Lig",
    "createdAt" : ISODate("2015-11-20T22:36:42.000Z")
}

/* 2 */
{
    "_id" : ObjectId("564fa081dd576ebbf90041ad"),
    "name" : "Premiership",
    "createdAt" : ISODate("2015-11-20T22:36:49.000Z"),
    "updatedAt" : ISODate("2015-11-20T22:37:33.000Z"),
    "teams" : [ 
        {
            "_id" : ObjectId("564fa0a6dd576ef2f80041ad"),
            "name" : "Arsenal",
            "createdAt" : ISODate("2015-11-20T22:37:26.000Z")
        }, 
        {
            "_id" : ObjectId("564fa0addd576ebaf90041ad"),
            "name" : "Liverpool",
            "createdAt" : ISODate("2015-11-20T22:37:33.000Z")
        }
    ]
}
Sign up to request clarification or add additional context in comments.

7 Comments

Wonderfull! Thank you very much.
No problem, enjoy. If it solves your problem, please accept it so that others can benefit from it. Thanks
Based on the example given for League document, how can you display the teams name in twig, I tried to do $var.products but doesnt work as it is an array. How does Symfony handle this please? I am only able to get the parent documents fields (name, age gender etc..) but not the products fields
You need to run foreach on $league and get team within the loop. Just a classic way if you. There are many of examples out there. Something like: foreach ($leagues as $league) { $team = $league->getTeam(); } so on. Just Google it.
Alright, thanks man! I'll try it once home, and sorry for that nooby questions...I am new to symfony :p
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.