duanqiao0153 2017-03-21 23:42
浏览 53
已采纳

试图调用一个名为“查询”类的未定义方法

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 used this code to edit the query in the Chat.php file

$sql = $this->container->get('database_connection');
$users = $sql->query("UPDATE user SET onOroff= '1' WHERE UserId='2'");

but the proplem when I call it in SocketCommand.php

new Chat($this->getContainer())

I get this error

Attempted to call an undefined method named "query" of class

the chat.php code

<?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;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class Chat extends Controller implements MessageComponentInterface  {
    protected $container;
    protected $clients;

    //protected $em;

    //protected $db;
    public function __construct(ContainerInterface $container) {
        $this->clients = new \SplObjectStorage;
        $this->container = $container;

    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})
";
        $sql = $this->container->get('database_connection');
        $users = $sql->query("UPDATE user SET onOroff= '1' WHERE UserId='2'");


    }


}

the SocketCommand.php full 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;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
// Change the namespace according to your bundle
use checkoomsBundle\Sockets\Chat;
use Doctrine\ORM\EntityManager;
class SocketCommand extends ContainerAwareCommand
{
    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($this->getContainer())
                )
            ),
            8080
        );

        $server->run();
    }
}

the services.yml code

services:
     database_connection:
         class: checkoomsBundle\Sockets\Chat
         arguments: ["@service_container"] 
  • 写回答

1条回答 默认 最新

  • douyu7618 2017-03-22 02:26
    关注
    services:
         database_connection:
             class: checkoomsBundle\Sockets\Chat
             arguments: ["@service_container"] 
    

    So you defined a service with id: database_connection that represents your Chat class

    $sql = $this->container->get('database_connection');
    

    And inside the same Chat class you try to get that service from container to execute sql query on it?

    Code you shared with us doesn't make sense. Take a closer look at your services definition.

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

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号