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 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题
  • ¥15 微信开发者工具vant组件