简体   繁体   English

Symfony2中的类继承

[英]Class inheritance in Symfony2

I'm a bit confused about an error that I'm getting: 我对收到的错误感到困惑:

Undefined method 'getAsArray'. 未定义的方法“ getAsArray”。 The method name must start with either findBy or findOneBy! 方法名称必须以findBy或findOneBy开头!

getAsArray() is a method in my repository class, it's called in PostsController.php like so: getAsArray()是我的存储库类中的方法,在PostsController.php这样调用它:

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

CategoryRepository.php is defined like this: CategoryRepository.php的定义如下:

namespace Air\BlogBundle\Repository;

class CategoryRepository extends TaxonomyRepository
{

}

It extends TaxonomyRepository that lives in the same namespace. 它扩展了TaxonomyRepository在相同名称空间中的TaxonomyRepository

TaxonomyRepository.php is defined like so: 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();
    }

}

Why am I getting the "undefined method getAsArray" error? 为什么会出现“未定义的方法getAsArray”错误?

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: 尝试将您的实体类的Entity注释修改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM