簡體   English   中英

symfony和ratchet在null上調用成員函數get()

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

我使用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'");



        }

}

您必須使用:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM