2017-03-21 27 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),我不確定這是否適用於您。無論如何,我的回答確實回答了你爲什麼會出現錯誤的問題,並且應該將其標記爲正確。如果你有另一個問題,你應該發表一個不同的問題。 –

相關問題