Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Call to a member function getId() on a non-object even though there is an object?

I keep getting this error:

    /* ... */
    $em = $this->getDoctrine()->getEntityManager();
    $movie = $em->getRepository('MyMyBundle:Movie')->findMovieByName('moviename'); // Repository Class
    \Doctrine\Common\Util\Debug::dump($movie); // dumps the object just fine! The Repository found it
    echo $movie->getId(); // brings me the error nevertheless

Fatal error: Call to a member function getId() on a non-object in ... on line ...

I have the getId() method in my Entity. Other methods also don't work even though it is and object!

The object gets dumped like:

..... array(1) { [0]=> object(stdClass)#759 (59) { ["__CLASS__"]=> string( .....

Any hints?

like image 297
Mike Avatar asked Dec 21 '22 19:12

Mike


1 Answers

Nevermind, I found the answer: echo $movie[0]->getId(); (the repository returned an array of objects). Sorry, sometimes you lose sight of the wood for the trees.

like image 115
Mike Avatar answered Dec 26 '22 16:12

Mike