0

我想在一个很慢的查询上运行解释,但我不知道如何查看原始 sql,所以我可以在 phpmyadmin 中运行它并调试它。这是功能。

    private function getAttImages($limit, $forumIds = 0, $fidsReverse = false, $topicIds = 0, $membersIds = 0, $order = 'attach_date', $sort = 'desc', $group = null)
{
    $fids = '';

    if ($forumIds)
    {
        $r = '';
        if ($fidsReverse)
        {
            $r = ' NOT ';
        }

        if (is_array($forumIds))
        {
            $forumIds = implode(',', $forumIds);
        }

        $fids = ' AND forums_topics.forum_id ' . $r . ' IN (' . $forumIds .  ')';
    }

    $tids = '';
    if ($topicIds)
    {
        $tids = ' AND forums_topics.tid IN (' . $topicIds . ')';
    }

    $mids = '';
    if ($membersIds)
    {
        $mids = ' AND core_attachments.attach_member_id IN (' . $membersIds . ')';
    }


    $whereT = array();
    $joinsT = array();

    $findInPosts = ' AND ' . \IPS\Db::i()->findInSet('queued', array('0'));
    $joinsT[] = array(
                        'select'    => 'forums_posts.*',
                        'from'      => 'forums_posts',
                        'where'     => array("forums_posts.pid=core_attachments_map.id2" . $findInPosts),
                );

    $findInTopics = ' AND ' . \IPS\Db::i()->findInSet('approved', array('1'));
    $joinsT[] = array(
                        'select'    => 'forums_topics.*',
                        'from'      => 'forums_topics',
                        'where'     => array("forums_topics.tid=forums_posts.topic_id" . $findInTopics . $fids . $tids),
                );

    $select = 'core_attachments.attach_id AS custom_data, core_attachments.*';
    if ($group)
    {
        $select = 'core_attachments.attach_id AS custom_data, COUNT(attach_is_image) as cnt_images, SUM(attach_hits) as summ_attach_hits, core_attachments.*';
    }

    $joinsT[] = array(
                        'select'    => $select,
                        'from'      => 'core_attachments',
                        'where'     => array('core_attachments.attach_is_image=1 AND core_attachments.attach_is_archived=0 AND core_attachments.attach_id=core_attachments_map.attachment_id' . $mids),

                );              

    $joinsT[] = array(  'select'    => 'core_members.member_id, core_members.member_group_id, core_members.mgroup_others, core_members.name, core_members.members_seo_name',
                        'from'      => 'core_members',
                        'where'     => array('core_attachments.attach_member_id=core_members.member_id' . $mids),
                );

    $joinsT[] = array(  'select'    => 'core_permission_index.perm_id',
                        'from'      => 'core_permission_index',
                        'where'     => array("core_permission_index.app='forums' AND core_permission_index.perm_type='forum' AND core_permission_index.perm_type_id=forums_topics.forum_id"),
                );

    $groupT = $group;

    $whereT[] = array(
        "core_attachments_map.location_key='forums_Forums' AND " . 
        \IPS\Db::i()->findInSet('perm_view', array_merge(array(\IPS\Member::loggedIn()->member_group_id), array_filter(explode(',', \IPS\Member::loggedIn()->mgroup_others)))) . " OR perm_view='*'" .
        $fids . $tids . $mids
    );

    $table = new \IPS\Helpers\Table\Db(
        'core_attachments_map',
        \IPS\Http\Url::internal('app=core&module=system&controller=nbattachpictures', 'front', 'nbattachpictures'),
        $whereT,
        $groupT
    );

    $table->joins = $joinsT;
    $table->sortBy = $order;
    $table->sortDirection = $sort;
    $table->limit = $limit;

    $table->rowsTemplate = array(\IPS\Theme::i()->getTemplate('plugins', 'core', 'global'), 'nbAttachmentsBlocksRows');

    $table->parsers = array(
        'custom_data' => function( $val, $row )
        {
            return array(
                'topic_data' => \IPS\Http\Url::internal("app=forums&module=forums&controller=topic&id={$row['tid']}", 'front', 'forums_topic', array($row['title_seo'])),
                'summ_attach_hits' => $row['summ_attach_hits'],
                'jewel' => $this->attachJewel($row['summ_attach_hits']),
            );
        },
    );

    return $table;
}

任何人都知道我如何才能看到仅由该函数生成的 SQL 查询?电子邮件比回声更好,因为我想从实时站点获取查询。

4

1 回答 1

1

您可以var_dump($table)使用本机 php 函数将结果写入电子邮件mail或将其写入日志文件(此选项更好)。

该框架是开源的吗?因为我找不到有关该类的任何文档\IPS\Helpers\Table\Db。可能其中有一个构建查询的方法,您可以在该类源代码中查找它,并将该方法的结果放入电子邮件消息或日志文件中,而不是 var_dump 表。

于 2016-01-27T03:15:28.860 回答