The method name must begin with findBy or findOneBy. Undefined symfony method?

I work through part4 of Symfony2 , and when updating the controller code and helper class I received the following error message

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

before I put some code in the controller, which I moved to my helper class, as described in the tutorial, resulting in an 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 a few lines from the /Entity/Blog.php file. see how correct they are according to 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?

+44
symfony
Feb 07 2018-12-12T00:
source share
8 answers

Make sure you change the entity class:

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

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

And don't forget to regenerate objects:

 php app/console doctrine:generate:entities Blogger 

UPDATE

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

+102
Feb 07 '12 at 8:26
source share

In my case, adding the correct annotation was not enough.

Removing the doctrine cache of php app/console doctrine:cache:clear-metadata did not work either.

I generate my entities from the database with commands

php app / console doctrine: mapping: import --force AcmeBlogBundle xml
php app / console doctrine: mapping: transform annotations. / src

The first command generates an orm.xml file for each database table in my project. After DELETING all the orm.xml files, Annotations started working correctly.

+8
Sep 05 '14 at 11:14
source share

If you use yml as configuration files for your entities, try adding this:

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

and then as above:

 php app/console doctrine:generate:entities Blogger 
+3
Jan 20 '14 at 20:08
source share

Another solution is to delete all orm.xml files added by the generated objects. If you move the folder or delete it, your mapping to the repository will work.

+2
Mar 22 '17 at 11:57
source share

If you use PHP-FPM, this problem may persist even after all of the above solutions that you tried to use using sudo service php5-fpm restart , which helped.

+1
Jun 10 '15 at 6:14
source share
  * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository") 

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

  * @ORM\Entity(repositoryClass="Blogger\BlogBundle\BlogRepository") 
0
Apr 30 '15 at 12:35
source share

For me it helped restart my vm (Vagrant Box)

0
Aug 28 '17 at 2:04 on
source share

In Symfony 3, you probably miss 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> 
0
Dec 14 '17 at 16:29
source share



All Articles