在Yii2中创建可扩展的网格视图

时间:2015-02-28 23:57:05

标签: yii2

我正在尝试在Yii2中创建一个可扩展的网格视图,但我遇到了一些问题。 我收到了这个警告:

PHP Warning – yii\base\ErrorException
reset() expects parameter 1 to be array, null given
 in C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\grid\DataColumn.php at line 129
   $provider = $this->grid->dataProvider;

    if ($this->label === null) {
        if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
            /* @var $model Model */
            $model = new $provider->query->modelClass;
            $label = $model->getAttributeLabel($this->attribute);
        } else {
            $models = $provider->getModels();
129         if (($model = reset($models)) instanceof Model) {
                /* @var $model Model */
                $label = $model->getAttributeLabel($this->attribute);
            } else {
                $label = Inflector::camel2words($this->attribute);
            }
        }
    } else {
        $label = $this->label;
    }

这是我的搜索模型代码:

<?php
namespace app\models;

use Yii;
use yii\data\ActiveDataProvider;
use yii\base\Model;

use app\models\Articles;
 /**
* 
*/
class ArticlesSearch extends Model
{
    /* your calculated attribute */
    public $article_num;
    public $title;
    public $jour_id;


    /* setup rules */
    public function rules() {
       return [
        /* your other rules */
        [['title'], 'safe']
       ];
    }


    public function search($params) {
        $query = Articles::find()->select('*')
                                ->where(['`journal_id`'=>$this->jour_id]);

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


        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        $query->orderBy('`articles`.`publication_date` ASC ');
        $query->andWhere(['LIKE','title',$this->title]);

       // $query->orFilterWhere(['like', '`publishers`.`name`', $this->name]);

        return $dataProvider;
    }
    public function getCount() 
    {

    }
    public function getModels() 
    {

    }
}

我在课程中添加了两个最后的方法来绕过以下错误:

Calling unknown method: app\models\ArticlesSearch::getCount()

Calling unknown method: app\models\ArticlesSearch::getModels()

我不知道为什么我班上需要这两种方法。我已经写了两个其他搜索模型,我没有把这些方法放在那里,它们工作得很好!!!

控制器代码:

$dataProvider=new ArticlesSearch();
$dataProvider->jour_id=$param['journalID'];
$searchModel= $dataProvider->search(Yii::$app->request->queryParams);
return $this->render('index', [  'searchModel' => $searchModel,
'dataProvider' => $dataProvider]);

查看代码:

<?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' =>$searchModel,
                    'columns' => [
                            [
                                'class'=>'kartik\grid\SerialColumn',
                                'contentOptions'=>['class'=>'kartik-sheet-style'],
                                //'width'=>'36px',
                                'header'=>'',
                                'headerOptions'=>['class'=>'kartik-sheet-style']
                            ],
                            [
                                'class'=>'kartik\grid\CheckboxColumn',
                                'headerOptions'=>['class'=>'kartik-sheet-style'],
                            ],
                            [
                                'class'=>'kartik\grid\ExpandRowColumn',
                                //'width'=>'50px',
                                'value'=>function ($model, $key, $index, $column) {
                                    return GridView::ROW_COLLAPSED;
                                },
                                'detail'=>function ($model, $key, $index, $column) {
                                   // return Yii::$app->controller->renderPartial('_expand-row-details', ['model'=>$articles]);
                                },
                                'headerOptions'=>['class'=>'kartik-sheet-style'] 
                                //'disabled'=>true,
                                //'detailUrl'=>Url::to(['/site/test-expand'])
                            ],
                            [
                                'attribute'=>'Title',
                                //'value' => $model->title,
                               // 'width'=>'410px',
                            ],
                        ],
                    ]);
?>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试修改此代码并查看其是否有效。我已经添加了一些评论来解释发生了什么。

<?php
namespace app\models;

use Yii;
use yii\data\ActiveDataProvider;
use app\models\Articles;

/**
** firstly, your search model needs to extend your original class, that was you have access to all the original attributes of your model, without having to declare them again.
**/
class ArticlesSearch extends Articles
{
    //If your original model already has these properties, you don't need to declare them again.
    public $article_num;
    public $title;
    public $jour_id;


/* Here you should declare rules for ALL the attributes that you want to use in search */
public function rules() {
   return [
    /* your other rules */
    [['title', 'jour_id', 'article_num'], 'safe']
   ];
}


public function search($params) {
    //Start by defining your basic search function
    $query = Articles::find();

    //Add in the dataProvider
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    //Start building the query that will be used to retrieve results
    $query->where(['jour_id' => $this->jour_id]);

    //Try to load the $params and validate them. If this fails, just return the dataProvider and do nothing else
    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    //If we're still in the method, carry on building the query
    $query->orderBy('`articles`.`publication_date` ASC ');

    //andFilterWhere() is better because it ignores empty values
    $query->andFilterWhere(['LIKE','title',$this->title]);

    return $dataProvider;
    }
    //Finally, remove the two extras functions you put in. If everything is working, you shouldn't need them.
}

接下来,在您的控制器中,使用此代码;

//Here you are telling Yii what model you want to use for searching, and for generating the form for the grid search.
$searchModel = new ArticlesSearch();

//I'm not sure where this $param is coming from.
$dataProvider->jour_id=$param['journalID'];

//Now you are actually setting up the dataProvider for the grid view. Notice that your $searchModel->search() method always returns a dataProvider, so this is the correct way to do it. Yii::$app->request->queryParams is loading the parameters that the search method will use for it's parameters.
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]);

现在您应该可以正常使用网格小部件,分别使用dataProvider和searchModel作为dataProvider和filterModel,但不要混淆它们!