I using symfony and ratchet web socket to connect to database and changing value in a certain column if someone connect to the server but I get error in this line
$conn = $this->get('database_connection');
Call to a member function get() on null
my services.yml file
services:
checkrooms.example:
class: check\roomsBundle\Sockets\Chat
arguments: ["@database_connection"]
calls:
- [setContainer, ['@service_container']]
my Chat.php code
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'");
}
}
You have to use:
$this->container->get('database_connection');
because you call method setContainer
and it looks like this:
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
$this->get()
is a shortcut only, you can use it if you extend base controller:
protected function get($id)
{
return $this->container->get($id);
}