2017-03-21 28 views
0

中调用我在使用ratchet和symfony 2.8连接到数据库并更改Web套接字应用程序中某列的值,如果有人连接到服务器,所以我应该注射服务,并添加EntityManager $emfunction __construct()这样的,但问题是,当我将它像上Chat.php文件类型错误:参数1通过Chat :: __ construc t()必须是Doctrine ORM EntityManager的一个实例,没有给出,在

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 

这个错误告诉我有这条线

new Chat() 

的chat.php文件都在文件SocketCommand.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(); 
    } 
} 
+0

这是您在过去两天中第三次询问同一个问题。每个人都有完全相同的答案。花点时间阅读依赖注入章节。请记住,new'ing与Symfony容器无关。 – Cerad

回答

0

出现的错误因为您已将构造函数定义为:

当你创建新的对象,像这样

$em = $this->getDoctrine()->getManager(); 

,然后通过这个:

,那么你就需要获得一个实体管理器这样的事情

new Chat($em) 

所以你”我需要弄清楚如何做到这一点。

+0

我收到通知:未定义变量:em – mrsharko

+0

您的'SocketCommand'类扩展命令,所以我不认为你可以用同样的方式获得一个EntityManager。看看[本文档](http://docs.doctrine-project.org/en/latest/reference/configuration.html#obtaining-an-entitymanager),我不确定这是否适用于您。无论如何,我的回答确实回答了你为什么会出现错误的问题,并且应该将其标记为正确。如果你有另一个问题,你应该发表一个不同的问题。 –

相关问题