3
votes

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);
    }
8
Did you ever solve this?Wilt

8 Answers

3
votes

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!

1
votes

I think the standard for repository classes is to put it in a subdirectory of the entity folder and still use the same entity namespace. Yours used a different namespace which is why I think you have the error.

According to the cookbook this is how the entity and custom respistory are defined. link to custom repository class in the cookbook.

Entity

// src/Acme/StoreBundle/Entity/Product.php
namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")
 */
class Product
{
    //...
}

Repository:

// src/Acme/StoreBundle/Entity/ProductRepository.php
namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ProductRepository extends EntityRepository
{
    public function findAllOrderedByName()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC'
            )
            ->getResult();
    }
}
1
votes

Fedor Petrick is right!

You should look for the orm file that corresponds to the entity. In my case, I have created a custom repository named: OfertaRepository.php in the folder OfertaBundle\Entity

On the other hand, I have a file Oferta.orm.xml

In line three , It said :

  <entity name="OfertaBundle\Entity\Oferta" table="oferta">

But it should be :

  <entity name="OfertaBundle\Entity\Oferta" table="oferta" repository-class="OfertaBundle\Entity\OfertaRepository">

Now, the method in the OfertaRepository.php works well!

1
votes

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

0
votes

Your code look correct in the Entity and Repository. Perhaps you could try to call the getRepository directly without ->getManager.

$this->getDoctrine->getRepository('FooNewsBundle:News')->test();

If you need a specific field you should have a look at the short notations with findOneBy and findBy in most cases its much easier instead of writing a custom class.

http://symfony.com/doc/current/book/doctrine.html

0
votes

Are you in production ? Perhaps clearing your cache is the solution :

php app/console cache:clear --env=prod --no-debug
0
votes

If clearing your cache doesn't work, is $em->getRepository('FooNewsBundle:News') instanceOf Foo\NewsBundle\Repository\NewsRepository true or false? By the looks of things, your not getting the correct repository somehow?

-1
votes

have you generated the entities?

php app/console doctrine:generate:entities BUNDLENAME

launch this command and then retry your code