ZF2 PDO无缓冲查询

时间:2013-11-28 09:03:24

标签: mysql pdo zend-framework2 unbuffered

我有一个安装方法,我的cms女巫读取sql文件并执行它们。到现在为止还挺好 .. 但我还需要对db进行一些初始化(插入一些动态值)

    $q = "INSERT INTO `tbl_users_roles` (`userId`, `roleId`) VALUES
            (1,3),
            (2,4);";
    $db->query($q, Adapter::QUERY_MODE_EXECUTE);

    $q = "INSERT INTO `tbl_users` (`username`, `password`, `accountStatus`) VALUES
            ('serverAdmin',?,1),
            ('admin',?,1);";
    $db->query($q, array($passAdmin, $passUser));

执行这些插入但下一个sql文件抛出异常:

PDOException: 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.

ZF2 PDO声明没有fetchAll或closeCursor。 那么如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

好吧我把最后一个查询改为:

    $st = $db->query($q);
    $st->execute(array(serialize($perm)))->getResource()->closeCursor();

这有效但其他数据库驱动程序也有closeCursor吗? 应该有一种通用的方法:(

答案 1 :(得分:0)

我已经用这种方式解决了:

$sql = "
    SET FOREIGN_KEY_CHECKS = 0;
    TRUNCATE TABLE `customer`;
    TRUNCATE TABLE `customer_address`;
    TRUNCATE TABLE `customer_contact`;
    SET FOREIGN_KEY_CHECKS = 1;
    ";

$result = $this->adapter->query($sql)->execute();
$result->getResource()->closeCursor();