user3757305
user3757305

Reputation: 179

Symfony2 doesn't seem to recognize my repository class

I am encountering the error Undefined method 'findAllCtrs'. The method name must start with either findBy or findOneBy!

I've tried all the other solutions on StackOverflow regarding this problem. I've cleared cache, cleared meta data cache, checked the namespaces and folder entities but still no fix.

Here is my entity:

namespace CFS\Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Ref
 *
 * @ORM\Table(name="ref", indexes={@ORM\Index(name="refno", columns={"refno"}), @ORM\Index(name="ctrno", columns={"ctrno"})})
 * @ORM\Entity(repositoryClass="CFS\Bundle\Entity\RefRepository")
 */
 class Ref
{

My repository class:

namespace CFS\Bundle\Entity;

use Doctrine\ORM\EntityRepository;

class RefRepository extends EntityRepository
{
  public function findAllCtrs()
  {
     $query = $this->getEntityManager()
        ->createQuery('
            SELECT
                r.refno, r.ctrno
            FROM
                CFSBundle:Ref r
            ORDER BY
                r.refno DESC
            ');

    try {
        return $query->getResult();
    } catch(\Doctrine\ORM\NoResultException $e) {
        return null;
    }
}

}

And I tried calling the method in my controller with:

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

    $containers = $em->getRepository('CFSBundle:Ref')
            ->findAllCtrs();

I did noticed that when I generate entities in the command line php app/console doctrine:generate:entities CFSBundle it is not recognizing my RefRepository.php file. What else could I have missed?

Upvotes: 2

Views: 2248

Answers (1)

Jonas Vogel
Jonas Vogel

Reputation: 418

I stuck with a similar error too, after spending a day searching for a solution I found a typo in my annotation referencing the repository class.
But I don't see a mistake in the code you provided, hence it should work…

Did you read https://stackoverflow.com/a/15184084/1781752 ?
There seemed to be problems mixing yml mappings and annotations.

Upvotes: 1

Related Questions