65

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 ?

ScoRpion
  • 11,364
  • 24
  • 66
  • 89
  • 3
    You also had a newline between the annotation and the blog class, it should be directly below. – solarc Feb 07 '12 at 11:53
  • Deleting the *.orm.xml files work for me. – Muhammad Shahzad Nov 19 '17 at 18:07
  • Its actually because of the **Access modifiers** in Repository if it is a private function you should start with `findBy, findOneBy` but as a good practice you **should not** make Repository functions public. _and Im using Symfony 4.3_ – Ahamed Rasheed Oct 15 '19 at 12:02

9 Answers9

156

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")

Molecular Man
  • 22,277
  • 3
  • 72
  • 89
  • Thanks for ur response. I have updated the question where i added Entity/Blog.php code. but i am still getting the error – ScoRpion Feb 07 '12 at 08:35
  • 3
    @Showket. Updated my answer. You have two `@ORM\Entity` annotations. The last one must be removed – Molecular Man Feb 07 '12 at 08:41
  • @MolecularMan: This solution is not working for Symfony 2.1.4. BlogRepository file is not generating. Is it a [bug](https://github.com/doctrine/DoctrineBundle/issues/124). Do you have any advice ! – Gowri Dec 13 '12 at 06:48
  • @gowri, it may be a bug. You can create repository file manualy if it's the only problem. – Molecular Man Dec 13 '12 at 07:55
9

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.

user4011534
  • 99
  • 1
  • 3
4

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
s1x
  • 574
  • 4
  • 15
2

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.

  • This worked for me. I had reverse engineered my entities from a MySQL database. Once I deleted the orm.xml files I was able to run the custom queries in the repository. – PrestonDocks Mar 22 '17 at 20:33
1

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.

Hbksagar
  • 548
  • 7
  • 17
0
 * @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")
K-Alex
  • 380
  • 1
  • 3
  • 17
0

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

Heisoka
  • 90
  • 6
  • 1
    I very much doubt that this is a **answer** to the question. If at all, this should be a comment. And you got enough reputation to comment! – GhostCat Aug 28 '17 at 14:09
  • @GhostCat for me it helped, so why shouldn't this be an answer? There is some issue with doctrine cache or something and this fixed it. I wish I would know more about this, but this solved my problem and I was searching for hours... – Heisoka Aug 29 '17 at 12:41
  • @Heisoka can you describe the source of the cache problem? – Borys Zielonka Oct 25 '21 at 09:58
0

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>
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
0

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

sh6210
  • 4,190
  • 1
  • 37
  • 27