试图调用名为" query"的未定义方法。班级

时间:2017-03-21 23:42:28

标签: php mysql database symfony websocket

我正在使用棘轮和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"] 

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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