Doctrine \ ORM \ EntityManager,未在类MessageComponentInterface中指定

问题描述 投票:1回答:1

我在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();

请帮助如何解决此问题

谢谢

php symfony ratchet
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.