Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 : Repository Class not found

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);
    }
like image 888
Napster Avatar asked Sep 12 '25 23:09

Napster


2 Answers

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!

like image 91
Fedir Petryk Avatar answered Sep 15 '25 13:09

Fedir Petryk


In your entity you are not using annotation, check if you have a news.yml file in Resources/config/doctrine

like image 43
LinChan Avatar answered Sep 15 '25 14:09

LinChan