Today I stuck in Repository Class Function I got this error
Undefined method 'test'. The method name must start with either findBy or findOneBy!
I allready checked these solutions - Solution 1 Solution 2 Solution 3
Is anything I need to add into config file ?
This is my Entity Class
// src/Foo/NewsBundle/Entity/News.php
namespace Foo\NewsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * News
 * @ORM\Entity(repositoryClass="Foo\NewsBundle\Repository\NewsRepository")
 * @ORM\Table(name="news")
 * @ORM\HasLifecycleCallbacks()
 */
class News
{
    /**
     * @var integer
     */
    private $id;
    /**
     * @var string
     */
    private $title;
This is my repository Class
// Foo/NewsBundle/Repository/NewsRepository.php
namespace Foo\NewsBundle\Repository;
use Doctrine\ORM\EntityRepository;
Class NewsRepository extends EntityRepository
{
    public function test()
    {
        return "Nisarg";
    }
}
And I am calling this test() function this wat from the controller
public function indexAction()
    {
//        $news = $this->getDoctrine()
//                ->getRepository('FooNewsBundle:News')
//                ->findAll();
        $em = $this->getDoctrine()
                   ->getManager();
        $news = $em->getRepository('FooNewsBundle:News')->test();
        if (!$news) {
            throw $this->createNotFoundException('No news found');
        }
        $build['news'] = $news;
        return $this->render('FooNewsBundle:Default:news_show_all.html.twig', $build);
    }
Check if you have specified your repository class in your News orm config file.
There must be somthing like "repositoryClass: Foo\NewsBundle\Repository\NewsRepository"
And don't forget to clear cache!
In your entity you are not using annotation, check if you have a news.yml file in Resources/config/doctrine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With