10

I'm following the Symfony2 tutorial (chapter 4), but I am having trouble retrieving the getLatestBlogs method from my custom repository.

I'm using Symfony 2.2 with Phar on Linux Mint.

I created the repository myself, but I am stumped. I get this error:

Undefined method 'getLatestBlogs'. The method name must start with either findBy or findOneBy! - BadMethodCallException

I have googled other similar questions but to no avail. Can anybody spot the error in my code?

Additional Info

My composer.json reads as follows :

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.2.0", ** NOTE : Originally read 2.2.* but I changed and successfully ran a composer update **
    "doctrine/orm": ">=2.2.3,<2.4-dev",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "1.0.*",
    "symfony/assetic-bundle": "2.1.*",
    "symfony/swiftmailer-bundle": "2.2.*",
    "symfony/monolog-bundle": "2.2.*",
    "sensio/distribution-bundle": "2.2.*",
    "sensio/framework-extra-bundle": "2.2.*",
    "sensio/generator-bundle": "2.2.*",
    "jms/security-extra-bundle": "1.4.*",
    "jms/di-extra-bundle": "1.3.*",
    "doctrine/doctrine-fixtures-bundle": "dev-master",
    "doctrine/data-fixtures" : "dev-master"        
},

My src/Blogger/BlogBundle/Controller/PageController.php:

namespace Blogger\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Blogger\BlogBundle\Entity\Enquiry;
use Blogger\BlogBundle\Form\EnquiryType;

class PageController extends Controller
{
    public function indexAction()
    {
        $em = $this->getDoctrine()
               ->getManager();

        $blogs = $em->getRepository('BloggerBlogBundle:Blog')->getLatestBlogs();

        return $this->render('BloggerBlogBundle:Page:index.html.twig', array(
            'blogs' => $blogs
        ));
    }

originally the lie and my src/Blogger/BlogBundle/Entity/Blog.php:

namespace Blogger\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="blog")
 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Blog
{

and finally my src/Blogger/BlogBundle/Repository/BlogRepository.php:

namespace Blogger\BlogBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * BlogRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class BlogRepository extends EntityRepository
{

    public function getLatestBlogs($limit = null)
    {
        $qb = $this->createQueryBuilder('b')
                   ->select('b')
                   ->addOrderBy('b.created', 'DESC');

        if (false === is_null($limit))
            $qb->setMaxResults($limit);

        return $qb->getQuery()
                  ->getResult();
    }

}
prime
  • 2,004
  • 3
  • 28
  • 45

4 Answers4

22

You can check the following to resolve this problem:

  • Make sure your FQCN in your annotation matches your classname of the repository file and you have set the right namespace.

  • Clear all metadata cache if you have this active or temporarily disable Doctrine caching.

    app/console doctrine:cache:clear-metadata

  • Check if your mapping type is set to annotation in your configuration. i.e. if you have yml you have to define your repositoryClass in the yml file.

Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
4

My problem was the fact that the generated xml files kept overriding the annotations. I have read that annotations and other type of configuration cannot reside together, but forgot that mine was still present.

Make sure you have no entity definitions (i.e. SomeEntity.orm.xml) in src/xxxx/xxxBundle/Resources/config/doctrine

  • Thanks, it was a useful tip. Doy you know how to avoid xml generation? – manuelbcd Feb 08 '16 at 08:01
  • I generated data from db do sf entity, first to xml and later I made conversion from xml to annotation and I was wondering why it doesn't want to work. Thanks to your answer I deleted all xml files and it started to work. – user3383675 Feb 19 '16 at 07:28
3

My solution was to change the order of Entity annotations:

/**
* Comments
* 
* @ORM\Entity(repositoryClass="CmsBundle\Entity\CommentsRepository")
* @ORM\Table()
* @ORM\Entity    
*/

didn't work, but

/**
* Comments
* 
* @ORM\Table()
* @ORM\Entity
* @ORM\Entity(repositoryClass="CmsBundle\Entity\CommentsRepository")
*/

did work :)

Fleskalebas
  • 71
  • 1
  • 7
  • 1
    The second `@ORM\Entity` will override the first, you don't need the one that doesn't define the `repositoryClass`. – DanielM Jun 05 '15 at 13:59
  • Oh yeah...that's why the first ordering went wrong, because the (second) empty Entity was 'resetting' the first one...seems logical! – Fleskalebas Jun 06 '15 at 16:06
-1

Try adding use Blogger\BlogBundle\Entity\Blog; to your controller.

keyboardSmasher
  • 2,661
  • 18
  • 20