1
votes

I know there are more questions about this problem, but I've tried many solutions and I can't resolve my problem.

As you can read, my error is the following:

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.

I think the problem may be I'm mixing connections in doctrine. My code is:

$em = $this->getDoctrine()->getManager(); 

$query_count = $em->createQueryBuilder('c')
    ->select('count(distinct c.id) as n_results')
    ->from('AppBundle:ClinicalCase', 'c')
    ->where('c.privacity = 1')
    ->andWhere('c.publication_date <= :today')
    ->setParameter('today', new \DateTime());

$n_results = $query_count->getQuery()->getResult();

$connection = $em->getConnection();

    $query = 'SELECT `clinical_case`.`id`, `social`.`id`, `t2`.`myCount`
        FROM `clinical_case` '.
        $favourites . '
        LEFT JOIN `tags_clinical_case`
        ON `clinical_case`.`id` = `tags_clinical_case`.`clinical_case_id`
        LEFT JOIN `social`
        ON `clinical_case`.`social_id`=`social`.`id`
        LEFT JOIN (
            SELECT `social_id`, count(`social_id`) AS `myCount`
            FROM `view`
            GROUP BY `social_id`
            ) t2
        ON `t2`.`social_id`= `social`.`id`
        WHERE `clinical_case`.`privacity_id`= 1'
        . $favourites_where .'
        GROUP BY `clinical_case`.`id`;
        ORDER BY ' .$order.'
        LIMIT '.$limit.', '.$clinical_cases_per_page;

    $statement = $connection->prepare($query);

    $statement->execute();

    $results = $statement->fetchAll();

    foreach ($results as $row) {
        $clinical_cases[] = $em->getRepository('AppBundle:ClinicalCase')->find($row['id']);
    }

I need a number of total results. That's why I'm doing the first query. Then, I need the real results with specifications. So, the result of second query is this:

    Array
(
    [0] => Array
        (
            [id] => 26
            [myCount] => 2
        )

    [1] => Array
        (
            [id] => 27
            [myCount] => 4
        )

    [2] => Array
        (
            [id] => 28
            [myCount] => 3
        )

)

After these results, I have the object's ids, so I just need to search in Doctrine with these ids.

foreach ($results as $row) {
            $clinical_cases[] = $em->getRepository('AppBundle:ClinicalCase')->find($row['id']);
        }

Here is where I get the error. So I think the problem is I am mixing many connections or something similar.

Thank you.

1
there is not enough information. where $query_count is supposed to run? - Your Common Sense
@YourCommonSense Sorry! I just need that count to know how many results are in the database. I've updated my question with that code line. - Jonathan Martínez
Well the error still seems highly unlikely. For the previous query you are using fetchAll() which makes it impossible for such an error to appear. - Your Common Sense
@YourCommonSense If i add this code: $statement->closeCursor(), the error dissapears, but then I can't access to clincial_cases in database with $em->getRepository('AppBundle:ClinicalCase')->find($row['id']); - Jonathan Martínez
what does it mean - you cannot? does it show any error? - Your Common Sense

1 Answers

1
votes

Finally I got the answer:

$statement = $connection->prepare($query);

$statement->execute();

$results = $statement->fetchAll();

$statement->closeCursor();