我正在开发一个网络套接字应用程序,使用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/