gpt4 book ai didi

php - Doctrine 和 Symfony2 : How to execute raw queries (General error: 2014 Cannot execute queries while > other unbuffered queries are active)

转载 作者:行者123 更新时间:2023-11-29 22:18:17 31 4
gpt4 key购买 nike

我正在尝试使用 Doctrine 在 Symfony 框架中执行原始查询。

这是代码:

class MessagesHandler
{
/** @var \Doctrine\Common\Persistence\ObjectManager The entity manager used to access entities */
protected $em = null;

public function __construct($em)
{
$this->em = $em;
}

public function getMessagesToPost($user)
{
$query = 'SELECT COUNT(*) FROM `messages` WHERE `posted_by` = ' . $user->getId() . ' AND `message_posted` = 0';

return $this->em->getConnection()->exec($query);
}

我已将 MessagesHandler 类设置为服务:

services:
shq.handler.Messages:
class: AppBundle\Handler\MessagesHandler
arguments: ["@doctrine.orm.entity_manager"]

现在,当我执行代码时,我收到以下错误:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.")...

如何使用 PDOStatement::fetchAll()

最佳答案

使用createNativeQuery方法:

$query = $entityManager->createNativeQuery('SELECT COUNT(*) FROM `messages` WHERE `posted_by` = ? AND `message_posted` = 0', $rsm);
$query->setParameter(1, $user->getId());

$count = $query->getResult();

此处提供文档:http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html

关于php - Doctrine 和 Symfony2 : How to execute raw queries (General error: 2014 Cannot execute queries while > other unbuffered queries are active),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31003515/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com