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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵