php - 尝试调用类中名为 "query"的未定义方法

标签 php mysql database symfony websocket

我正在开发一个网络套接字应用程序,使用ratchet和symfony 2.8连接到数据库,并在有人连接到服务器时更改特定列中的值,因此我使用此代码来编辑Chat.php文件中的查询

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

但是当我在SocketCommand.php中调用它时出现问题

new Chat($this->getContainer())

我收到此错误

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

chat.php代码

<?php
namespace check\roomsBundle\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})\n";
        $sql = $this->container->get('database_connection');
        $users = $sql->query("UPDATE user SET onOroff= '1' WHERE UserId='2'");


    }


}

SocketCommand.php完整代码

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

services.yml 代码

services:
     database_connection:
         class: check\roomsBundle\Sockets\Chat
         arguments: ["@service_container"] 

最佳答案

services:
     database_connection:
         class: check\roomsBundle\Sockets\Chat
         arguments: ["@service_container"] 

因此,您定义了一个 id 为 database_connection 的服务,它代表您的 Chat

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

在同一个 Chat 类中,您尝试从容器获取该服务以在其上执行 SQL 查询?

您与我们分享的代码没有意义。仔细查看您的服务定义。

关于php - 尝试调用类中名为 "query"的未定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940218/

相关文章:

mysql - 我需要的是多左外连接和冷融合吗?

php - 保存 PHP 应用程序设置的最佳方法是什么?

php - 无法通过 openshift 发送电子邮件

mysql - 正则表达式复制粘贴行并在每个行的末尾添加递增的数字

php - XML - 创建元素 - 换行

mysql - 查询返回错误结果,预期一行但得到全部

mysql - 如何执行从不同表返回唯一值的 mySQL 查询

mysql - SQL:查询整个数据库

php - 通过 SSL 的非阻塞套接字连接?

php - 在 Laravel 中找到交点的最佳方法