I'm trying to execute a raw query using Doctrine into the Symfony framework.
Here there is the code:
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);
}
I have set the class MessagesHandler as a service:
services:
shq.handler.Messages:
class: AppBundle\Handler\MessagesHandler
arguments: ["@doctrine.orm.entity_manager"]
Now, when I execute the code, I receive the following error:
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.")...
How can I use PDOStatement::fetchAll()
?