在字符串上调用成员函数getRepository()


Call to a member function getRepository() on string

我创建了symfony服务,如下所述:Symfony2: get Doctrine在一个通用的PHP类

Service.yml

app.lib.helpers:
    class: AppBundle'Lib'Helpers
    arguments: [doctrine.orm.entity_manager]

类助手

class Helpers
{
    protected $em;
    public function __construct($entityManager)
    {
        $this->em = $entityManager;
    }
    public function checkStatuses()
    {
        $orderId = $this->em->getRepository('AppBundle:Orders')->findAll();
    }
}

In controller action

$helper = $this->get('app.lib.helpers');
$helper->checkStatuses();

但是我得到错误:

错误:在字符串

上调用成员函数getRepository()

是什么导致了这个问题?

我觉得应该是

arguments: [ "@doctrine.orm.entity_manager" ]

in your Service.yml

您的服务定义不正确,试试这个或者您可以阅读这个以获得更多解释http://symfony.com/doc/current/service_container.html:

app.lib.helpers:
    class: AppBundle'Lib'Helpers
    arguments: [@doctrine.orm.entity_manager]