Yii2 ActiveDataProvider获取结果正确但在摘要文本

时间:2016-08-11 10:20:46

标签: php yii2

我正在尝试使用下拉列表过滤数据,因此完美执行的查询产生的结果始终是正确的,但总的来说,总计数是' 0'总是但不是第一次加载页面时。这让我很烦,我完全无法通过调试工具跟踪错误。

但重要的是,每当我将相同的代码部署到生产中时,相同的代码在我的本地计算机上运行良好它显示0计数

我的型号代码:

public function search($params) {

    $query = Tasks::find();

    $query->where(['q_id' => $this->job_id]);

    // $query->orderBy('created_at DESC');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => 20,
        ],
    ]);


    $this->load($params);

    if (!empty($this->keyword) || $this->keyword != '') {

        $query->andWhere('MATCH(task_title,task_description, priority) AGAINST("' . $this->keyword . '*" IN BOOLEAN MODE)');
    }

    if (is_numeric($this->filterby)) {
        $query->andFilterWhere(['status' => $this->filterby]);
    } else {
        $query->andFilterWhere(['IN', 'status', [10, 4, 2]]);
    }

    if (!empty($this->sortby) || $this->sortby != '') {
        $query->orderBy($this->sortby . ' DESC');
    }

    return $dataProvider;
}

这是我的观看代码:

<?php
yii\widgets\Pjax::begin(['id' => 'tasks-pjax', 'timeout' => 10000]);
echo \yii\widgets\ListView::widget([
    'layout' => '{summary}<br/>{items}{pager}',
    'dataProvider' => $dataProvider,
    'summary' => '<div class="col-lg-12 hidden-xs"><p class="text-muted text-success"> {totalCount} Tasks Found!</p></div>',
    'summaryOptions' => ['style' => 'margin-bottom: 5px;margin-top:5px;', 'tag' => 'span'],
    'id' => 'tasks-list',
    'itemOptions' => ['class' => 'task-item'],
    'emptyText' => 'No Task Found !',
    'emptyTextOptions' => ['class' => 'list-group-item', 'style' => 'margin-left: 15px; margin-right:15px; color: red'],
    'itemView' => '_tasks',
    'pager' => [
        'class' => 'kop\y2sp\ScrollPager',
        'container' => '#tasks-list',
        'item' => '.task-item',
        // 'next' => '.next a',
        'triggerOffset' => 20,
        'noneLeftText' => '',
    ]
])
?>
<?php yii\widgets\Pjax::end(); ?>

控制器代码:

public function actionIndex() {

    $searchModel = new \frontend\models\TasksSearch();

    if (Yii::$app->request->get('q_id')) {
        $qModel = QPosts::findOne(['q_id' => base64_decode(Yii::$app->request->get('q_id'))]);
        $searchModel->q_id = $qModel->q_id;

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        if (Yii::$app->user->identity->id == $qModel->user_id || Yii::$app->user->identity->id == $xxxModel->xxx_id) {
            return $this->render('index', [
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,
                        'qModel' => $qModel,
            ]);
        } else {
            return $this->redirect(['xxxx/xxxx', 'id' => $qModel->q_id]);
        }
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

2 个答案:

答案 0 :(得分:0)

你可以发布网格代码吗?周围有pjax吗?您$dataProvider->models中的内容是什么?

我注意到的其他一些事情,没有回答你原来的问题:

而不是

$query->orderBy($this->sortby . ' DESC');

$query->orderBy([$this->sortby => SORT_DESC]);

或我正在处理的状态(打开/关闭/挂起/取消,活动=打开或待处理,非活动=关闭或取消):

<?php
namespace common\models;
use Yii;

class Status
{
    const UNDEFINED = 0;
    const OPEN      = 1;
    const CLOSED    = 2;
    const CANCELLED = 4;
    const PENDING   = 8;

    const ACTIVE    = 9;
    const INACTIVE  = 6;


    static function asArray() {
        return [
            self::OPEN      => Yii::t('app', 'Open'),
            self::CLOSED    => Yii::t('app', 'Closed'),
            self::CANCELLED => Yii::t('app', 'Cancelled'),
            self::PENDING   => Yii::t('app', 'Pending'),
        ];
    }

    static function asFilter() {
        return [
            self::ACTIVE    => Yii::t('app', 'Active'),
            self::INACTIVE  => Yii::t('app', 'Inactive'),
            self::OPEN      => Yii::t('app', 'Open'),
            self::CLOSED    => Yii::t('app', 'Closed'),
            self::CANCELLED => Yii::t('app', 'Cancelled'),
            self::PENDING   => Yii::t('app', 'Pending'),
        ];
    }

}
函数搜索中的

($ params):

if (!empty($this->status))
    $query->andWhere('task.status & :status = task.status', [':status' => $this->status]);

自定义StatusColumn:

<?php
namespace common\grid;

use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;
use yii\web\View;
use yii\helpers\Url;
use common\models\Status;

class StatusColumn extends \yii\grid\DataColumn {

    public $attribute      = 'status';
    public $headerOptions  = ['class' => 'text-center', 'style' => 'width: 10em'];
    public $contentOptions = ['class' => 'text-center'];
    public $footerOptions  = ['class' => 'text-center'];

    private $sum1 = 0;
    private $sum2 = 0;

    public function renderDataCell($model, $key, $index) {
        $value      = $model->__get($this->attribute);
        $this->sum1 += ($value === null ? 0 : ($value & Status::ACTIVE ? 1 : 0));
        $this->sum2 += ($value === null ? 0 : ($value & Status::INACTIVE ? 1 : 0));
        return parent::renderDataCell($model, $key, $index);
    }

    protected function renderFooterCellContent() {
        $this->footer = sprintf('%d/%d', $this->sum1, $this->sum2);
        return parent::renderFooterCellContent();
    }

    public function init() {
        parent::init();

        if ($this->content === null) {
            $this->content = function($data, $key, $index) {
                $value = $data->__get($this->attribute);

                switch ($value) {
                    case Status::OPEN :
                        return sprintf('<div class="label label-primary">%s</div>', Yii::t('app', 'Open'));
                        break;

                    case Status::CLOSED :
                        return sprintf('<div class="label label-success">%s</div>', Yii::t('app', 'Closed'));
                        break;

                    case Status::CANCELLED :
                        return sprintf('<div class="label label-danger">%s</div>', Yii::t('app', 'Cancelled'));
                        break;

                    case Status::PENDING :
                        return sprintf('<div class="label label-info">%s</div>', Yii::t('app', 'Pending'));
                        break;

                    default:
                        return sprintf('<div class="label label-default">%s</div>', Yii::t('app', 'undefined'));
                        break;
                }

                return $value;
            };
        }

        if ($this->filter === null) {
            $this->filter = Status::asFilter(); 
        }

        if ($this->footer === null) {
            $sum1 = 0;
            $sum2 = 0;
            $this->footer = sprintf('%d/%d', $this->sum1, $this->sum2);
        }
    }
}

?>

并在网格中使用它:

    [
        'class' => 'common\grid\StatusColumn',
    ],

答案 1 :(得分:0)

我有类似的问题。我的activerecord查询有一个joinWith,我忘了在查询中添加groupBy,所以它是重复行。我正在使用ListView小部件,它使{items} {pager}或{summary}错误(我忘了)。无论如何使用joinWith在我的查询中添加groupBy解决了我的问题。