S
S
Svetlana Galenko2019-02-02 22:18:54
Yii
Svetlana Galenko, 2019-02-02 22:18:54

What caused this error 'The "dataProvider" property must be set.'?

Hello, I can't figure out why this error occurs:

Invalid Configuration – yii\base\InvalidConfigException
The "dataProvider" property must be set.

    1. in /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/widgets/BaseListView.php

Here is the SupportController.php controller:
<?php

namespace app\controllers\user;

use app\components\Controller;
use app\helpers\UserSettingHelper;
use app\models\User;
use app\models\UserFriend;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
use app\models\TicketBody;
use app\models\TicketFile;
use app\models\TicketHead;
use app\models\UploadForm;
use yii\filters\AccessControl;
use yii\filters\AccessRule;
use yii\helpers\Url;
use yii\web\UploadedFile;

class SupportController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class'      => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules'      => [
                    [
                        'actions' => ['index', 'view', 'open'],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
        ];
    }
    
    /**
     * Renders the index view for the module
     *
     * @return string
     */
    public function actionIndex()
    {
        $this->layout = 'page';
        $vars = [];

        $user = User::find()->where(['username' => $_GET['username']])->one();
        if ($user === null)
            throw new NotFoundHttpException();

        $setting = UserSettingHelper::get($user);
        
        $vars = ArrayHelper::merge($vars, [
            'user' => $user,
            'setting' => $setting,
        ]);
        
        $dataProvider = (new TicketHead())->dataProviderUser();
        Url::remember();

        return $this->render('index', $vars, ['dataProvider' => $dataProvider]);
    }
}

Here is the view:
<div>
                                <?= \yii\grid\GridView::widget([
                                    'dataProvider' => $dataProvider,
                                    'rowOptions'   => function ($model) {
                                        return ['data-id' => $model->id, 'class' => 'ticket'];
                                    },
                                    'columns'      => [
                                        'department',
                                        'topic',
                                        [
                                            'contentOptions' => [
                                                'style' => 'text-align:center;',
                                            ],
                                            'value'          => function ($model) {
                                                switch ($model['status']) {
                                                    case TicketHead::OPEN :
                                                        return '<div class="label label-default">Открыт</div>';
                                            case TicketHead::WAIT :
                                                return '<div class="label label         -warning">Ожидание</div>';
                                            case TicketHead::ANSWER :
                                                return '<div class="label label         -success">Отвечен</div>';
                                            case TicketHead::CLOSED :
                                                return '<div class="label label         -info">Закрыт</div>';
                                           }
                                    },
                                    'format'         => 'html',
                                    ],
                                    [
                                        'contentOptions' => [
                                            'style' => 'text-align:right; font-size:13px',
                                        ],
                                        'attribute'      => 'date_update',
                                        'value'          => "date_update",
                                    ],
                                    ],
                                ]) ?>
                            </div>

TicketHead.php model:
<?php

namespace app\models;

use \app\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\data\ActiveDataProvider;
use yii\db\Expression;
use app\models\Module;

/**
 * This is the model class for table "ticket_head".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $department
 * @property string $topic
 * @property integer $status
 * @property string $date_update
 */
class TicketHead extends \yii\db\ActiveRecord
{
    /**
     * dataProvider для пользователей
     *
     * @return ActiveDataProvider
     */
    public function dataProviderUser()
    {
        $query = TicketHead::find()->where("user_id = " . Yii::$app->user->id);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'date_update' => SORT_DESC
                ]
            ],
            'pagination' => [
                'pageSize' => $this->module->pageSize
            ]
        ]);

        return $dataProvider;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Anisimov, 2019-02-02
@swallow_97

return $this->render('index', $vars, ['dataProvider' => $dataProvider]);

the render method takes only 2 parameters, and you have 3 of them from somewhere, merge your variable with the provider into the $vars array, but it's better to get rid of the extra variable altogether
$this->layout = 'page';

$user = User::find()->where(['username' => $_GET['username']])->one();
if ($user === null) {
     throw new NotFoundHttpException();
}

$setting = UserSettingHelper::get($user);
        
$dataProvider = (new TicketHead())->dataProviderUser();
       
Url::remember();

return $this->render('index', [
    'user' => $user,
    'setting' => $setting,
    'dataProvider' => $dataProvider
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question