luqo33
luqo33

Reputation: 8361

Class inheritance in Symfony2

I'm a bit confused about an error that I'm getting:

Undefined method 'getAsArray'. The method name must start with either findBy or findOneBy!

getAsArray() is a method in my repository class, it's called in PostsController.php like so:

$categoriesList = $this->getDoctrine()->getRepository('AirBlogBundle:Category')->getAsArray();

CategoryRepository.php is defined like this:

namespace Air\BlogBundle\Repository;

class CategoryRepository extends TaxonomyRepository
{

}

It extends TaxonomyRepository that lives in the same namespace.

TaxonomyRepository.php is defined like so:

namespace Air\BlogBundle\Repository;

use Doctrine\ORM\EntityRepository;


class TaxonomyRepository extends EntityRepository {

    public function getQueryBuilder(array $params = array()) {
        $qb = $this->createQueryBuilder('t');

        $qb->select('t, COUNT(p.id) as postsCount')
                ->leftJoin('t.posts', 'p')
                ->groupBy('t.id');

        return $qb;
    }

    public function getAsArray() {
        return $this->createQueryBuilder('t')
                        ->select('t.id, t.name')
                        ->getQuery()
                        ->getArrayResult();
    }

}

Why am I getting the "undefined method getAsArray" error?

Upvotes: 2

Views: 80

Answers (1)

Maltronic
Maltronic

Reputation: 1802

Usually this error occurs when you forget to specify the repository class inside your entity class. Try modifying to the Entity annotation of your entity class to:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\CategoryRepository")
 */
class Category {}

Upvotes: 3

Related Questions