dongtuan5367 2017-03-21 02:21
浏览 70
已采纳

类型错误:参数1传递Chat :: __ construc t()必须是Doctrine \ ORM \ EntityManager的实例,没有给定,调用

I working on a web socket app using ratchet and symfony 2.8 to connect to database and changing value in a certain column if someone connect to the server so I should injection the service and add the EntityManager $em to function __construct() like that but the problem is when i adding it like that on Chat.php file

public function __construct(EntityManager $em) 

I get this error

  [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 

this error tell me there are a problem on file SocketCommand.php on this line

new Chat()

the chat.php file

<?php
namespace checkoomsBundle\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})
";
        //$this->em->getRepository('yorrepo')->updateFuntion();
        $sql = $this->container->get('database_connection');
        $users = $sql->query("UPDATE user SET ONoroff= '1999' WHERE UserId='2'");


    }
}

the SocketCommand.php code

<?php
// myapplication/src/sandboxBundle/Command/SocketCommand.php
// Change the namespace according to your bundle
namespace checkoomsBundle\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 checkoomsBundle\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();
    }
}
  • 写回答

1条回答 默认 最新

  • dongxing6802 2017-03-21 05:04
    关注

    The error occurs because you've defined the constructor as:

    public function __construct(EntityManager $em) {
        $this->clients = new \SplObjectStorage;
        //$this->container = $container;
        $this->em = $em;
    }
    

    So then you'll need to get an entity manager something like this:

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

    and then pass this in when you create your new object like so:

    new Chat( $em )
    

    So you'll need to figure out how to do that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能