Find referenced documents using MongoDB, Symfony2 - ReferenceMany

645 views
Skip to first unread message

Nebumix

unread,
Mar 4, 2014, 8:52:36 AM3/4/14
to mongod...@googlegroups.com
Hello everybody, I am studying mongodb using Symfony2. I tried to reference some objects inside each other using ReferenceMany.

I state that almost everything works, I can insert objects and extract them, the only thing that I can not do is retrieve the encapsulated objects , I do not understand why.

http://paste.ubuntu.com/7033041/

http://paste.ubuntu.com/7033055/

I can insert objects easily doing so, I have a graphical feedback of everything that happens because I use phpmoadmin to check.

http://paste.ubuntu.com/7033058/

To find datas I do so:

http://paste.ubuntu.com/7033061/

The main problem is that the function
->getProva()
return an empty array

If I check ot the database using phpMoAdmin, I have this

http://paste.ubuntu.com/7033062/

Using my code I have this:

http://paste.ubuntu.com/7033066/

Unfortunately I cannot figure out how to find referenced objects.
In the documentation I found on line, I read that you have just to call the function
->getProva()


But it returns an empty array.

Some idea where is my mistake?
Thanks


s.molinari

unread,
Mar 5, 2014, 2:38:54 AM3/5/14
to mongod...@googlegroups.com
Hi,

Disclaimer: I am also learning Mongo with PHP, so everything here could be completely wrong. LOL!.;)

It seems you are using Doctrine's MongoDB ODM, correct? If so, you could just use the methods (like get and set) from the ODM itself, right? 

     /**
     * @MongoDB\Document
     */
    class Test
    {
        /**
         * @MongoDB\Id
         */
        private $id;

        /**
         * @MongoDB\Collection
         */
        private $foo = array();

        /** @MongoDB\ReferenceMany(targetDocument="Prova") */
        private $prova = array();
         }

     /**
     * @MongoDB\Document
     */
    class Prova
    {
        /**
         * @MongoDB\Id
         */
        private $id;

        /**
         * @MongoDB\string
         */
        private $message;

        /**
         * @MongoDB\string
         */
        private $city;
    }

    public function getAction($dm, $id)
    {
            $qb = $dm->createQueryBuilder('Test')    
                ->field('prova')->prime(true)
                ->findOneBy(array('id'=>$id);
            //prime(true) = get references in one additional db query.  
            $query = $qb->getQuery();
            $test = $query->execute();
                   
            if (!$test) {
                return $this->render('AcmeProvaBundle:Default:prova.html.twig');
            }
           
            foreach ($test->getProvas() as $prova) {

                   //your code here to manipulate $pro ???

            }
            // or if you only want the object.

            $pro = $test->getProvas(); 
                
            return $this->render('AcmeProvaBundle:Default:prova.html.twig', array('test' => $test, 'pro' => $pro));

    }

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/priming-references.html    

Notice I also changed the class name Prove to Prova. I am not sure if that is really needed, but I think for naming conventions within Doctrine ODM, it seems better/ simpler.

Again, I am just learning too, so this could all be wrong.:) 

Scott

Nebumix

unread,
Mar 5, 2014, 7:47:19 AM3/5/14
to mongod...@googlegroups.com
Hi, thank you very much, I have written my question in 3 group and 1 forum and only you replied me.
I tried to do how you suggest, but I have the same problem, not improve.

Do you have some example of working code to show me?

Thanks a lot.

s.molinari

unread,
Mar 5, 2014, 10:49:52 AM3/5/14
to mongod...@googlegroups.com
No, no working code for you. Sorry.

Have you looked to see if there is a Prova collection and is there data in it?

Scott

Mario Alicicco

unread,
Mar 5, 2014, 11:00:44 AM3/5/14
to mongod...@googlegroups.com

Yes, absolutely, there are documents Prova. I can't understand why my array is empty. Now I try to create a new symfony project, starting from zero and trying to do it again. Maybe I installed badly some component.
If you have some advise, please let me know.
Thanks a lot.


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/j-_o3lJC0nU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Mario Alicicco

s.molinari

unread,
Mar 5, 2014, 11:19:36 AM3/5/14
to mongod...@googlegroups.com
To be honest, I am at the same point as you are and am just putting Symfony together with Mongodb for our project.

Scott

Nebumix

unread,
Mar 5, 2014, 11:58:02 AM3/5/14
to mongod...@googlegroups.com
I built the project from zero and now it is working.
I used the code you suggested me.

Thank you very much.

s.molinari

unread,
Mar 5, 2014, 12:48:48 PM3/5/14
to mongod...@googlegroups.com
Well, that is good to hear. Glad I could help.:)

Scott
Reply all
Reply to author
Forward
0 new messages