![](/img/trans.png)
[英]Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called
[英]Type error: Argument 1 passed Chat::__construc t() must be an instance of Doctrine\ORM\EntityManager, none given, called in
我正在使用棘轮和symfony 2.8开发Web套接字应用程序,以连接到数据库并在有人连接到服务器的情况下更改特定列中的值,因此我应该注入服务并将EntityManager $em
添加到function __construct()
,但是问题是当我在Chat.php
文件上添加它时
public function __construct(EntityManager $em)
我得到这个错误
[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
此错误告诉我此行上的SocketCommand.php
文件有问题
new Chat()
chat.php文件
<?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;
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})\n";
//$this->em->getRepository('yorrepo')->updateFuntion();
$sql = $this->container->get('database_connection');
$users = $sql->query("UPDATE user SET ONoroff= '1999' WHERE UserId='2'");
}
}
SocketCommand.php代码
<?php
// myapplication/src/sandboxBundle/Command/SocketCommand.php
// Change the namespace according to your bundle
namespace check\roomsBundle\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 check\roomsBundle\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();
}
}
发生错误是因为您已将构造函数定义为:
public function __construct(EntityManager $em) {
$this->clients = new \SplObjectStorage;
//$this->container = $container;
$this->em = $em;
}
因此,您需要获得一个实体管理器,如下所示:
$em = $this->getDoctrine()->getManager();
然后在创建新对象时将其传入,如下所示:
new Chat( $em )
因此,您需要弄清楚该如何做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.