The method name must start with either findBy or findOneBy. Undefined method Symfony?

Symfony

Symfony Problem Overview


I am working through part4 of Symfony2, and while updating the controller and helper class code i got the following error message

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

before i had put some code in controller that i shifted to my helper class as taught by tutorial, which result in the above error message.

<?php
// 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();
  } 
}

And here is my controller file index Action Code:-

// src/Blogger/BlogBundle/Controller/PageController.php
class PageController extends Controller
{
  public function indexAction()
  {
    $em = $this->getDoctrine()
               ->getEntityManager();

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

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

    // ..
}

I am attaching few lines from /Entity/Blog.php file. please see if they are correct as per your answer.

<?php
// src/Blogger/BlogBundle/Entity/Blog.php

namespace Blogger\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

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

class Blog
 {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @ORM\HasLifecycleCallbacks()
  */
  protected $id;
  --
  --
 }

Where Am I doing wrong ?

Symfony Solutions


Solution 1 - Symfony

Make sure that you have modified your entity class:

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

the annotation @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository") is required.

And don't forget to regenerate entities:

php app/console doctrine:generate:entities Blogger

UPDATE

Remove annotation @ORM\Entity. It overrides correct annotation @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")

Solution 2 - Symfony

In my case adding proper annotation was insufficient.
Deleting Doctrine Cache by php app/console doctrine:cache:clear-metadata also not worked.

I generate my entities from database by commands >php app/console doctrine:mapping:import --force AcmeBlogBundle xml
>php app/console doctrine:mapping:convert annotation ./src
php app/console doctrine:generate:entities AcmeBlogBundle

First command generate orm.xml file for each DB Table in my project. After DELETING all orm.xml files Annotations started work properly.

Solution 3 - Symfony

If youre using yml as config files for your entities try adding this:

Blogger\BlogBundle\Entity\Blog:
    type: entity
    table: Blog
    repositoryClass: Blogger\BlogBundle\Repository\BlogRepository
    ...

and then as mentioned above:

php app/console doctrine:generate:entities Blogger

Solution 4 - Symfony

The other solution is to delete all the orm.xml files added by generated entities. If you move the folder or delete, your mapping with repository will be operationnal.

Solution 5 - Symfony

In case you are using PHP-FPM then this issue might persist even after all the above solutions you have tried then use sudo service php5-fpm restart which did the trick for me.

Solution 6 - Symfony

 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")

Try putting the repository class at the same directory next to the Entity class:

     * @ORM\Entity(repositoryClass="Blogger\BlogBundle\BlogRepository")

Solution 7 - Symfony

For me it helped to restart my vm (Vagrant Box)

Solution 8 - Symfony

In Symfony 3 you are probably missing the repository class in your orm.xml file.

repository-class="Bundle\Repository\MyRepository"
Example:
<doctrine-mapping>
    <entity name="Bundle\Entity\MyEntity"
            table="tablename"
            repository-class="Bundle\Repository\MyRepository">
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>
    </entity>
</doctrine-mapping>

Solution 9 - Symfony

In my case, there was no method in that particular place. After placing/creating the according method the error was gone.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScoRpionView Question on Stackoverflow
Solution 1 - SymfonyMolecular ManView Answer on Stackoverflow
Solution 2 - Symfonyuser4011534View Answer on Stackoverflow
Solution 3 - Symfonys1xView Answer on Stackoverflow
Solution 4 - SymfonyBenjamin PetitView Answer on Stackoverflow
Solution 5 - SymfonyHbksagarView Answer on Stackoverflow
Solution 6 - SymfonyK-AlexView Answer on Stackoverflow
Solution 7 - SymfonyHeisokaView Answer on Stackoverflow
Solution 8 - Symfonytobias47n9eView Answer on Stackoverflow
Solution 9 - Symfonysh6210View Answer on Stackoverflow