教义\ ORM \ EntityManager,在类MessageComponentInterface中未指定

时间:2019-11-26 11:23:36

标签: php symfony ratchet

我在symfony 2.8的项目中使用Ratchet websocket库。 我的问题是如何定义存在于另一个控制器方法中的实体管理器。

class WesocketController implements MessageComponentInterface  {
protected $clients;

public function __construct() {
    $this->clients = new \SplObjectStorage;
}
public function onMessage(ConnectionInterface $from, $msg) {
    echo $msg;
    $numRecv = count($this->clients) - 1;
    echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
        , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        $myController = new OtherController();
        $myController->test(); 

    foreach ($this->clients as $client) {
        if ($from !== $client) {
            // The sender is not the receiver, send to each client connected
            $client->send($msg);
        }
    }
}

我在其他控制器中的功能:

public function test(){
   // echo('yassine');

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

       $newTest = new VisitsTest();
            $newVisitsTest
                ->setTestDate(new \DateTime())
                ->setClient(null);
            $em->persist($newTest);
            $em->flush();
}

错误:

PHP致命错误:未捕获的错误:MyApp / vendor / symfony / symfony / src / Symfony / Bundle / FrameworkBundle / Controller / Controller.php中对成员函数has()的调用为null

该错误在另一个控制器的这一行上 $ em = $ this-> getDoctrine()-> getManager();

请帮助解决此问题

谢谢

1 个答案:

答案 0 :(得分:0)

对于控制器操作,请考虑Autowirring your services

public function test(EntityManagerInterface $em){
       $newTest = new VisitsTest();
            $newVisitsTest
                ->setTestDate(new \DateTime())
                ->setClient(null);
            $em->persist($newTest);
            $em->flush();
}

那样,您不会为要调用的每个动作打开一个新的EntityManager