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

时间:2017-03-19 09:45:02

标签: php database symfony websocket ratchet

我使用symfony和棘轮Web套接字连接到数据库并在某个列中更改值,如果有人连接到服务器但我在此行中出错

        $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'");



        }

}

1 个答案:

答案 0 :(得分:1)

你必须使用:

$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