0

I have already read the existing pages here regarding the same error message declared in the title. Like for example this here:

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

However, i did not find working solution. I have done everything according to the manual:

http://symfony.com/doc/3.0/book/doctrine.html#custom-repository-classes

I'm using annotation based configuration and here is the start of the food class:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Food
 *
 * @ORM\Table(name="food")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\FoodRepository")
 */

However, the command:

php bin/console doctrine:generate:entities AppBundle

does not generate the FoodRepository repo, so i had to do it manually. Here is the code of the FoodRepository:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

class FoodRepository extends EntityRepository
{
    public function findFoodsByName()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT f FROM AppBundle:Food f WHERE f.foodname LIKE :food'
            )
            ->getResult();

    }
}

and here is the slice from the controller:

$em = $this->getDoctrine()->getManager();

        $foods = $em->getRepository('AppBundle:Food')
            ->findFoodsByName();

I have also tried to empty the Doctrine cache with command:

php app/console doctrine:cache:clear-metadata

without results. The problem persists.

Has anyone had the same problem with Symfony and what did you do to solve the problem?

Community
  • 1
  • 1
user3629146
  • 107
  • 2
  • 12
  • I'm not sure if it will solve the entire issue, but yes you do need to rename your method to match the Symfony expected method names. Instead of "findFoodsByName" it should be something like "findByFoodName" – user1934587390 Jun 15 '16 at 11:59
  • With the help of your comment, this particular problem was solved, and few new ones introduced. Nevertheless, thank you very much for your advice. – user3629146 Jun 15 '16 at 12:51
  • Strange because @Chris 's assertion that you had to use the the Doctrine naming convention is wrong. I wonder if you cleaned out some files under Resouces/config/doctrine? Or if did go with findBy then I suspect you are still not using your custom repository. – Cerad Jun 15 '16 at 15:25

0 Answers0