SQLSTATE [HY000]:常规错误:2014在其他未缓冲的查询处于活动状态时无法执行查询

时间:2017-03-21 08:41:06

标签: php mysql symfony doctrine-orm

我知道有关此问题的更多问题,但我已经尝试了很多解决方案,而我无法解决问题。

正如您所读,我的错误如下:

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.

我认为问题可能是我在学说中混合连接。我的代码是:

$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']);
    }

我需要一些总结果。这就是我进行第一次查询的原因。然后,我需要有规格的实际结果。所以,第二个查询的结果是:

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

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

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

)

在这些结果之后,我有了对象的ID,所以我只需要在Doctrine中搜索这些ID。

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

这是我收到错误的地方。所以我认为问题是我混合了许多连接或类似的东西。

谢谢。

1 个答案:

答案 0 :(得分:0)

最后我得到了答案:

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

$statement->execute();

$results = $statement->fetchAll();

$statement->closeCursor();