I working on a web socket app using ratchet and symfony 2.8 to connect to database and changing value in a certain column if someone connect to the server so I should injection the service and add the EntityManager $em
to function __construct()
like that but the problem is when i adding it like that on Chat.php
file
public function __construct(EntityManager $em)
I get this error
[Symfony\Component\Debug\Exception\FatalThrowableError]
Type error: Argument 1 passed Chat::__construc t() must be an instance of Doctrine\ORM\EntityManager, none given, called in SocketCommand.php on line 41
this error tell me there are a problem on file SocketCommand.php
on this line
new Chat()
the chat.php file
<?php
namespace checkoomsBundle\Sockets;
use tuto\testBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManager;
class Chat implements MessageComponentInterface {
//private $container;
protected $clients;
protected $em;
//protected $db;
public function __construct(EntityManager $em) {
$this->clients = new \SplObjectStorage;
//$this->container = $container;
$this->em = $em;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})
";
//$this->em->getRepository('yorrepo')->updateFuntion();
$sql = $this->container->get('database_connection');
$users = $sql->query("UPDATE user SET ONoroff= '1999' WHERE UserId='2'");
}
}
the SocketCommand.php code
<?php
// myapplication/src/sandboxBundle/Command/SocketCommand.php
// Change the namespace according to your bundle
namespace checkoomsBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
// Include ratchet libs
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
// Change the namespace according to your bundle
use checkoomsBundle\Sockets\Chat;
class SocketCommand extends Command
{
protected function configure()
{
$this->setName('sockets:start-chat')
// the short description shown while running "php bin/console list"
->setHelp("Starts the chat socket demo")
// the full command description shown when running the command with
->setDescription('Starts the chat socket demo')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln([
'Chat socket',// A line
'============',// Another line
'Starting chat, open your browser.',// Empty line
]);
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
}
}