我使用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'");
}
}
答案 0 :(得分:1)
你必须使用:
$this->container->get('database_connection');
因为您调用方法setContainer
,它看起来像这样:
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
$this->get()
只是一个快捷方式,如果扩展基本控制器,可以使用它:
protected function get($id)
{
return $this->container->get($id);
}