Symfony2

时间:2015-06-17 12:08:58

标签: php symfony doctrine-orm

我对我得到的错误感到有点困惑:

  

未定义的方法'getAsArray'。方法名称必须以其中一个开头   findBy或findOneBy!

getAsArray()是我的存储库类中的一个方法,它在PostsController.php中调用,如下所示:

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

CategoryRepository.php的定义如下:

namespace Air\BlogBundle\Repository;

class CategoryRepository extends TaxonomyRepository
{

}

它扩展了TaxonomyRepository,它位于同一名称空间中。

TaxonomyRepository.php的定义如下:

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();
    }

}

为什么我收到“未定义的方法getAsArray”错误?

1 个答案:

答案 0 :(得分:3)

通常,当您忘记在实体类中指定存储库类时会发生此错误。尝试修改实体类的实体注释:

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