我使用棘轮和symfony 2.8在Web套接字应用程序上工作,以连接到数据库,如果有人连接到服务器,则会更改某个列中的值,所以我应该注入服务并添加EntityManager $em
像这样function __construct()
,但问题是当我像这样Chat.php
文件上添加它时
public function __construct(EntityManager $em)
我收到此错误
[SymfonyComponentDebugExceptionFatalThrowableError]
Type error: Argument 1 passed Chat::__construc t() must be an instance of DoctrineORMEntityManager, none given, called in SocketCommand.php on line 41
此错误告诉我此行的文件SocketCommand.php
有问题
new Chat()
聊天.php文件
<?php
namespace checkroomsBundleSockets;
use tutotestBundleEntityUsers;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
use SymfonyComponentDependencyInjectionContainerInterface;
use DoctrineORMEntityManager;
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})n";
//$this->em->getRepository('yorrepo')->updateFuntion();
$sql = $this->container->get('database_connection');
$users = $sql->query("UPDATE user SET ONoroff= '1999' WHERE UserId='2'");
}
}
套接字命令.php代码
<?php
// myapplication/src/sandboxBundle/Command/SocketCommand.php
// Change the namespace according to your bundle
namespace checkroomsBundleCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
// Include ratchet libs
use RatchetServerIoServer;
use RatchetHttpHttpServer;
use RatchetWebSocketWsServer;
// Change the namespace according to your bundle
use checkroomsBundleSocketsChat;
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();
}
}
发生此错误是因为您已将构造函数定义为:
public function __construct(EntityManager $em) {
$this->clients = new SplObjectStorage;
//$this->container = $container;
$this->em = $em;
}
因此,您需要获得如下所示的实体管理器:
$em = $this->getDoctrine()->getManager();
然后在创建新对象时将其传入,如下所示:
new Chat( $em )
所以你需要弄清楚如何做到这一点。