试图调用名为“createQueryBuilder”的未定义方法

时间:2016-10-02 10:41:09

标签: symfony

我正在尝试了解如何在服务中执行QueryBuilder,所以这就是我所做的:

创建我的服务:

namespace OC\PlatformBundle\PurgerAdvert;

use Doctrine\ORM\EntityManager;

class PurgerAdvert
{

    private  $em;

    public function __construct(EntityManager $em) {
        $this->entityManager = $em;

    }

    public function purge($days)
    {

$queryBuilder = $this->createQueryBuilder();
$queryBuilder->select('a')->from('OCPlatformBundle:Advert', 'a');

// get the Query from the QueryBuilder here ...
$query = $qb->getQuery();
return $result = $query->getResult();
    }   

}

在services.yml中声明它:

  oc_platform.purger.advert:
      class: OC\PlatformBundle\PurgerAdvert\PurgerAdvert
      arguments: ['@doctrine.orm.entity_manager']

我删除了查询,因为我之前有一个错误:"Attempted to call an undefined method named "createQueryBuilder" of class "OC\PlatformBundle\PurgerAdvert\PurgerAdvert""我认为我没有正确地调用实体管理员,但我没有看到我的错误......

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您可能不想一直输入entityManager,因此两个答案的组合应该有效:

namespace OC\PlatformBundle\PurgerAdvert;

use Doctrine\ORM\EntityManager;

class PurgerAdvert
{
    private  $entityManager;

    public function __construct(EntityManager $em) {
      $this->entityManager = $em;
    }

    public function purge($days)
    {
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder->select('a')->from('OCPlatformBundle:Advert', 'a');

        // get the Query from the QueryBuilder here ...
        $query = $qb->getQuery();
        return $result = $query->getResult();
    }
}

尝试一下,我认为它应该有用。