symfony 和 ratchet 在 null 上调用成员函数 get()

symfony and ratchet Call to a member function get() on null

我使用 symfony 和棘轮网络套接字连接到数据库,如果有人连接到服务器,我会更改特定列中的值,但我在这一行中出错

        $conn = $this->get('database_connection');

在 null 时调用成员函数 get()

我的 services.yml 文件

services:
     checkrooms.example:
         class: check\roomsBundle\Sockets\Chat
         arguments: ["@database_connection"]
         calls:
             - [setContainer, ['@service_container']]

我的Chat.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; 


    class Chat extends Controller implements MessageComponentInterface  {
        protected $clients;
        public function __construct() {
            $this->clients = new \SplObjectStorage;

        }



        public function onOpen(ConnectionInterface $conn) {
            $this->clients->attach($conn);
    //database part
            echo "New connection! ({$conn->resourceId})\n";
            $conn = $this->get('database_connection');
            $users = $conn->query("UPDATE user SET Batman= '1999' WHERE UserId='2'");



        }

}

你必须使用:

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

因为您调用了方法 setContainer,它看起来像这样:

    public function setContainer(ContainerInterface $container = null)
{
    $this->container = $container;
}

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/ContainerAwareTrait.php#L31

$this->get()只是一个快捷方式,如果你扩展基本控制器就可以使用它:

  protected function get($id)
{
    return $this->container->get($id);
}

https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php#L410