【问题标题】:Call to a member function getRepository() on string调用字符串上的成员函数 getRepository()
【发布时间】:2016-11-09 12:06:58
【问题描述】:

我创建了如下所述的 symfony 服务:Symfony2: get Doctrine in a generic PHP class

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

    }
}

在控制器动作中

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

但我得到错误:

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

是什么导致了这个问题?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    我觉得应该是的

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

    在您的 Service.yml 中

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-05-10
        • 2018-08-26
        • 2016-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多