【问题标题】:Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to [...]::__construct() must implement interface [...] none givenSymfony2:ContextErrorException:可捕获的致命错误:传递给 [...]::__construct() 的参数 1 必须实现接口 [...] 没有给出
【发布时间】:2014-03-02 13:07:54
【问题描述】:

问题:

每当我尝试访问应用程序时都会收到此错误:

ContextErrorException:可捕获的致命错误:参数 1 传递给 PL\OrderBundle\Entity\OrderHasComment::__construct() 必须实现 接口 Symfony\Component\Security\Core\SecurityContextInterface, 没有给出,调用 /var/www/html/apps/portal_de_logistica/vendor/sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php 在第 416 行并在 /var/www/html/apps/portal_de_logistica/src/PL/OrderBundle/Entity/OrderHasComment.php 第 48 行

我做错了什么?

【问题讨论】:

    标签: php symfony dependency-injection doctrine-orm symfony-2.4


    【解决方案1】:

    PL\OrderBundle\Entity\OrderHasComment 的构造函数要求一个强制参数,但在创建对象的新实例时不提供它。

    您正在像这样创建一个新的 OrderHasComment(无论是什么):

    $object = new OrderHasComment() // <- missing argument 
    

    删除它 - 一旦你的听众调用类似setContext(...) 的东西,它就不再需要了,并且不需要创建对象......所以无论如何它不应该是强制性的。

    // remove the mandatory argument or provide a default (i.e. $context = null)
    public function __construct(ContextInterface $context) /
    {
        // ...
    

    ...应该变成:

    public function __construct()
    {
    

    这解决了导致异常的问题。

    【讨论】:

    • 我解决了你告诉我的问题,但现在我收到了这个错误ServiceCircularReferenceException: Circular reference detected for service "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager -&gt; doctrine.dbal.default_connection -&gt; orderhascomment.listener -&gt; security.context -&gt; security.authentication.manager -&gt; fos_user.user_manager -&gt; fos_user.entity_manager".
    • 另外,我可以这样做来设置用户吗? public function onPrePersist(LifecycleEventArgs $args) { $entity = $args-&gt;getEntity(); $entity-&gt;setUser(); } 或者这是完全错误的?
    • onPrePersist 实现看起来不错,但还有另一个问题涉及您注入 security.context 服务。我可以很容易地帮助你......但是......我的回答显然解决了这个问题(这个问题中要求的异常问题)。请先投票/接受答案,然后针对您的新例外提出新问题。这就是stackoverflow的工作原理……否则页面将是一团糟,没有人能够快速找到具体问题的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2014-01-29
    相关资源
    最近更新 更多