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

时间:2016-10-11 17:55:34

标签: php mysql pdo

我试图了解PDO细节。所以我编码了这个:

<?php
namespace news\system\worker;
use news\data\news\NewsEditor;
use wcf\data\object\type\ObjectTypeCache;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\search\SearchIndexManager;
use wcf\system\user\activity\point\UserActivityPointHandler;
use wcf\system\worker\AbstractRebuildDataWorker;
use wcf\system\WCF;

/**
 * Worker actions for news entries.
 * 
 * @author  Pascal Bade
 * @copyright   2013 voolia.de
 * @license Creative Commons CC-BY-ND <http://creativecommons.org/licenses/by-nd/3.0/deed.de>
 * @package de.voolia.news
 */
class NewsDataWorker extends AbstractRebuildDataWorker {
    /**
     * @see \wcf\system\worker\AbstractRebuildDataWorker::$objectListClassName
     */
    protected $objectListClassName = 'news\data\news\NewsList';

    /**
     * @see \wcf\system\worker\AbstractWorker::$limit
     */
    protected $limit = 250;

    /**
     * @see \wcf\system\worker\AbstractRebuildDataWorker::initObjectList
     */
    protected function initObjectList() {
        parent::initObjectList();

        $this->objectList->sqlOrderBy = 'news.newsID';
    }

    /**
     * @see \wcf\system\worker\IWorker::execute()
     */
    public function execute() {
        parent::execute();

        if (!count($this->objectList)) {
            return;
        }

        if (!$this->loopCount) {
            // remove the activity points
            UserActivityPointHandler::getInstance()->reset('de.voolia.news.activityPointEvent.news');

            // remove the entry from search index
            SearchIndexManager::getInstance()->reset('de.voolia.news.entry');
        }

        // get news attachments
        $attachmentObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.attachment.objectType', 'de.voolia.news.entry');
        $sql = "SELECT      COUNT(*) AS attachments
            FROM        wcf".WCF_N."_attachment
            WHERE       objectTypeID = ?
            AND     objectID = ?";
        $attachments = WCF::getDB()->prepareStatement($sql);

        // calculate the cumulative likes
        $conditions = new PreparedStatementConditionBuilder();
        $conditions->add("objectID IN (?)", array($this->objectList->getObjectIDs()));
        $conditions->add("objectTypeID = ?", array(ObjectTypeCache::getInstance()->getObjectTypeIDByName('com.woltlab.wcf.like.likeableObject', 'de.voolia.news.likeableNews')));

        $sql = "SELECT  objectID,
                cumulativeLikes
            FROM    wcf".WCF_N."_like_object
            ".$conditions;
        $statement = WCF::getDB()->prepareStatement($sql);
        $statement->execute($conditions->getParameters());
        $likes = array();
        while ($row = $statement->fetchArray()) {
            $likes[$row['objectID']] = $row['cumulativeLikes'];
        }

        // update the news entries
        $userItems = array();
        foreach ($this->objectList as $news) {
            // new EntryEditor
            $editor = new NewsEditor($news);

            // update search index
            SearchIndexManager::getInstance()->add('de.voolia.news.entry', $news->newsID, $news->message, $news->subject, $news->time, $news->userID, $news->username, $news->languageID);

            // news data
            $newsData = array();

            // likes
            $newsData['cumulativeLikes'] = (isset($likes[$news->newsID])) ? $likes[$news->newsID] : 0;

            // attachments
            $attachments->execute(array($attachmentObjectType->objectTypeID, $news->newsID));
            $row = $attachments->fetchArray();
            $newsData['attachments'] = $row['attachments'];

            if ($news->userID) {
                if (!isset($userItems[$news->userID])) {
                    $userItems[$news->userID] = 0;
                }
                $userItems[$news->userID]++;
            }

            $editor->update($newsData);
        }

        // update activity points
        UserActivityPointHandler::getInstance()->fireEvents('de.voolia.news.activityPointEvent.news', $userItems, false);
    }
}

但我收到了这个

  

SQLSTATE [HY000]:常规错误:2014在其他未缓冲的查询处于活动状态时无法执行查询。考虑使用PDOStatement :: fetchAll()。或者,如果您的代码只是针对mysql运行,则可以通过设置PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性来启用查询缓冲。

有人可以为此做点什么吗?

1 个答案:

答案 0 :(得分:-1)

我收到此错误:

SQLSTATE [HY000]:常规错误:2014在其他未缓冲的查询处于活动状态时无法执行查询。考虑使用PDOStatement :: fetchAll()。或者,如果您的代码只是针对mysql运行,则可以通过设置PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性来启用查询缓冲。

堆栈跟踪:

0 /var/www/clients/client1/web5/web/wcf/lib/data/DatabaseObjectEditor.class.php(68):wcf \ system \ database \ Database-&gt; prepareStatement(&#39; UPDATE \ tnews1_ne ...&#39)

1 /var/www/clients/client1/web5/web/news/lib/system/worker/NewsDataWorker.class.php(108):wcf \ data \ DatabaseObjectEditor-&gt; update(Array)

2 /var/www/clients/client1/web5/web/wcf/lib/acp/action/WorkerProxyAction.class.php(93):news \ system \ worker \ NewsDataWorker-&gt; execute()

3 /var/www/clients/client1/web5/web/wcf/lib/action/AbstractAction.class.php(49):wcf \ acp \ action \ WorkerProxyAction-&gt; execute()

4 /var/www/clients/client1/web5/web/wcf/lib/action/AJAXInvokeAction.class.php(63):wcf \ action \ AbstractAction-&gt; __ run()

5 /var/www/clients/client1/web5/web/wcf/lib/system/request/Request.class.php(58):wcf \ action \ AJAXInvokeAction-&gt; __ run()

6 /var/www/clients/client1/web5/web/wcf/lib/system/request/RequestHandler.class.php(139):wcf \ system \ request \ Request-&gt; execute()

7 /var/www/clients/client1/web5/web/acp/index.php(10):wcf \ system \ request \ RequestHandler-&gt; handle(&#39; wbb&#39;,true)< / H1>

8 {main}