2

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?

4
  • Does using this method with AirBlogBundle:Taxonomy repository work? Commented Jun 17, 2015 at 12:12
  • I've now got rid of the inheritance all together and put the methods directly in CategoryReposiotry.php and am still getting the error. Is it possible that the erro is caused by the fact that PostsController.php lives in AirAdminBundle while CategoryReposiotry lives in AirBlogBundle ? I'm not sure, I'm referring to the repo with getRepository('AirBlogBundle:Category') which should find the repository in another bundle. Commented Jun 17, 2015 at 12:24
  • AFAIK, having its repo in another bundle than the controller shouldn't be a problem. Also, can you update your question with your newly created code? Commented Jun 17, 2015 at 12:25
  • Did you forget to add repositoryClass to @ORM\Entity annotation? Commented Jun 17, 2015 at 12:26

1 Answer 1

3

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 {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.