H
H
hollanditkzn2017-07-06 13:34:18
Yii
hollanditkzn, 2017-07-06 13:34:18

How to make it display the appropriate number of records?

It turns out for me that I write one code and I get 0 entries, although I know that there are 9 entries. I have code in widget

class Counter extends Widget
{
    private $params;

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

        $zakaz = Zakaz::find();
$this->view->params['scoreZakazAdmin'] = $zakaz->andWhere(['action' => 1])->count();
        $this->view->params['scoreZakazShop'] = $zakaz->andWhere(['id_sotrud' => Yii::$app->user->id, 'action' => 1])->count();
$this->view->params['scoreDisain'] = $zakaz->andWhere(['status' => [Zakaz::STATUS_DISAIN, Zakaz::STATUS_SUC_DISAIN, Zakaz::STATUS_DECLINED_DISAIN], 'action' => 1])->count();
}
public function run()
    {
        return Nav::widget([
        'options' => ['class' => 'nav nav-pills headerNav'],
        'items' => [
['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreZakazAdmin'].'</span>', 'encode' => false, 'url' => ['zakaz/admin'], 'visible' => Yii::$app->user->can('seeAdmin')],
            ['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreZakazShop'].'</span>', 'encode' => false, 'url' => ['zakaz/shop'], 'visible' => Yii::$app->user->can('seeShop')],
['label' => 'Заказы <span class="badge pull-right">'.$this->view->params['scoreDisain'].'</span>', 'encode' => false, 'url' => ['zakaz/disain'], 'visible' => Yii::$app->user->can('disain')],
]
 ]);
    }
}

In the debug panel, I see an inappropriate query
SELECT COUNT(*) FROM `zakaz` WHERE (`action`=1) AND ((`id_sotrud`=3) AND (`action`=1)) AND ((`status` IN ( 3, 4, 9)) AND (`action`=1))
C:\OpenServer\domains\crm\frontend\components\Counter.php:37
C:\OpenServer\domains\crm\frontend\views\layouts\main .php:36
C:\OpenServer\domains\crm\frontend\controllers\ZakazController.php:625
13:21:03.242 info yii\db\Command::query SELECT COUNT(*) FROM `zakaz` WHERE (`action` =1) AND ((`id_sotrud`=3) AND (`action`=1)) AND ((`status` IN (3, 4, 9)) AND (`action`=1)) AND ((`status ` IN (6, 7, 10)) AND (`action`=1))
C:\OpenServer\domains\crm\frontend\components\Counter.php:38
C:\OpenServer\domains\crm\frontend\views\ layouts\main.php:36
C:\OpenServer\domains\crm\frontend\controllers\ZakazController.php:625
Although if you remove the scoreZakazShop, It will be the norm

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-07-06
@hollanditkzn

$zakaz = Zakaz::find();
$zakaz->andWhere(['action' => 1])->count();
//вот тут $zakaz уже содержит результат count
//соответственно дальнейший $zakaz->andWhere должен вызывать ошибку, но у Вас они отключены.

so at least
$zakaz = Zakaz::find();
$this->view->params['scoreZakazAdmin'] = $zakaz->andWhere(['action' => 1])->count();
$zakaz = Zakaz::find();
$this->view->params['scoreZakazShop'] = $zakaz->andWhere(['id_sotrud' => Yii::$app->user->id, 'action' => 1])->count();
$zakaz = Zakaz::find();
$this->view->params['scoreDisain'] = $zakaz->andWhere(['status' => [Zakaz::STATUS_DISAIN, Zakaz::STATUS_SUC_DISAIN, Zakaz::STATUS_DECLINED_DISAIN], 'action' => 1])->count();
}

Turn on the output of all errors, otherwise development will become hell. You need to know when the program does not work correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question