【问题标题】:symfony An error has occurred: Notice: Undefined property: ::$containersymfony 发生错误:注意:未定义的属性:::$container
【发布时间】:2017-03-20 02:25:25
【问题描述】:

如果有人连接到服务器,我正在使用棘轮和 symfony 2.8 连接到数据库并更改特定列中的值的 Web 套接字应用程序,但我在这一行中遇到错误

    $sql = $this->container->get('database_connection');

完整的错误信息

发生错误:注意:未定义属性:check\roomsBundle\Sockets\Chat::$container

我在 services.yml 代码中的注入

services:
     database_connection:
         class: check\roomsBundle\Sockets\Chat
         arguments: ["@service_container"] 

我的 Chat.php 代码

<?php
namespace check\roomsBundle\Sockets;
use tuto\testBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Chat implements MessageComponentInterface  {


    protected $clients;
    //protected $db;
    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";

        $sql = $this->container->get('database_connection');
        $users = $sql->query("UPDATE user SET ONoff= '1' WHERE UserId='2'");


    }
}

【问题讨论】:

    标签: php database symfony sockets websocket


    【解决方案1】:

    好的,您需要解决一些问题才能解决您的问题。

    services:
         database_connection:
             class: check\roomsBundle\Sockets\Chat
             arguments: ["@service_container"] 
    

    这是在调用构造函数时,它将传入服务容器,但是使用构造函数传入你的容器是不利的,而是你应该实现Symfony\Component\DependencyInjection\ContainerAwareInterface接口,然后实现方法setContainer 和可选的getContainer 方法。

    /**
     * @param ContainerInterface|NULL $container
     */
    public function setContainer(
        ContainerInterface $container = NULL
    )
    {
        $this->container = $container;
        return $this;
    }
    
    /**
     * @return ContainerInterface
     */
    protected function getContainer()
    {
        return $this->container;
    }
    

    然后更新您的服务以在初始化时调用此方法。

    services:
         chat_service: # renamed because this is your chat service, not your database connection
             class: check\roomsBundle\Sockets\Chat
             calls:
                 - [setContainer, ["@service_container"]]
    

    【讨论】:

    • 我得到这个错误 inerAwareInterface::setContainer() cannot contain body in
    • 您想完成收到的错误信息吗?
    • PHP 致命错误:接口函数 Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer() cannot contain body in /opt/lampp/htdocs/x/1/sym/thesym/vendor/symfony/symfony /src/Symfony/Component/DependencyInjection/ContainerAwareInterface.php 第 24 行
    • 这一行的错误 public function setContainer( ContainerInterface $container = NULL )
    • 好像你修改了这个文件。 vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ContainerAwareInterface.php,您应该永远修改vendor 文件夹中的任何内容。我提供的更改不应该在 chat.php 文件或它继承的其他类中使用 extends 关键字进行。
    【解决方案2】:

    你的服务很好,你只需要对你的 chat.php 类做些小改动

    use Symfony\Component\DependencyInjection\ContainerInterface as Container;
    
    class Chat implements MessageComponentInterface  {
    
    protected $clients;
    private $container;
    //protected $db;
    public function __construct(Container $container) {
        $this->clients = new \SplObjectStorage;
        $this->container = $container;
    }
    

    现在你可以使用$this-&gt;container

    更新

    尝试注入实体管理器

    services:
      database_connection:
        class: check\roomsBundle\Sockets\Chat
        arguments: 
            - @doctrine.orm.default_entity_manager
    

    在chat.php中这样做

    use Doctrine\ORM\EntityManager;
    class Chat implements MessageComponentInterface  {
    
       protected $clients;
       protected $em;
       public function __construct(EntityManager $em) {
           $this->clients = new \SplObjectStorage;
           $this->em = $em;
       }
    $this->em->getRepository('yorrepo')->updateFuntion();
    

    现在尝试从某个 repo 调用来更新

    【讨论】:

    • 我收到此错误 [Symfony\Component\Debug\Exception\FatalThrowableError] 类型错误:参数 1 传递给 check\roomsBundle\Sockets\Chat::__construct t() 必须实现接口 rInterface,没有给出, 调用
    【解决方案3】:

    注入服务容器一般认为是bad idea.

    你应该考虑注入database_connection服务。

    有几种方法可以做到这一点。看看Types of Injection.

    services:
         chat_service:
             class: check\roomsBundle\Sockets\Chat
             arguments: ["@database_connection"]
    

    你的班级

    protected $connection;
    
    public function __construct($connection) {
        $this->connection = $connection;
    }
    

    即使你想继续注入服务容器,上面的链接也有相关的文档,可以帮助你解决你面临的问题。

    【讨论】:

    • 添加了示例代码,要求投反对票的用户评论他们这样做的原因。
    • 感谢您的回答我在尝试您的代码时收到此错误 [Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException] 检测到服务“database_connection”的循环引用,路径:“database_connection -> database_connection” .
    • 在答案的示例中,服务 ID 应该不同于 database_connection。您还可以更改注入服务的 id。但是,由于类名与聊天有关,因此服务不应被命名为与数据库连接相关的名称。
    • @xabbuh,感谢 xabbbuh.totally 在我发布分析器时错过了循环引用错误。与您的评论无关:在添加代码示例之前,我的回答被否决了,我想由于没有示例,它被否决了。
    • [Symfony\Component\Debug\Exception\ContextErrorException] 警告:在 /opt/lampp/htdocs/x/1 中调用 check\roomsBundle\Sockets\Chat::__construct () 缺少参数 1 /sym/thesym/src/check/roomsBun dle/Command/SocketCommand.php 在第 41 行并定义
    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2016-11-12
    • 2011-03-05
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多