Symfony2: Call to a member function getId() on a n

2019-02-23 23:01发布

问题:

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?

回答1:

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.