未知属性– yii \ base \ UnknownPropertyException

时间:2019-05-28 12:22:53

标签: php gridview yii2 filtering

尝试在var data = { 1: "hello", 2: "world" } 中显示关系字段时,标题出现错误。 我遇到了gridview的问题。

我想念什么? 预先感谢!

我的Projectsearch模型:

statuscode.statusname

我的观点:

<?php namespace frontend\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Project;

class ProjectSearch extends Project
{
/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        [['id', 'client_id', 'status_id'], 'integer'],
        [['number', 'name'], 'safe'],
        [['client.name'], 'safe'],
        [['statuscode.statusname'], 'safe'],
    ];
}

/**
 * {@inheritdoc}
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

public function attributes()
{
    // add related fields to searchable attributes
    return array_merge(parent::attributes(), ['client.name']);
    return array_merge(parent::attributes(), ['statuscode.statusname']);
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Project::find();

    // add conditions that should always apply here

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

    $dataProvider->sort->attributes['client.name'] = [
        'asc' => ['client.name' => SORT_ASC],
        'desc' => ['client.name' => SORT_DESC],
    ];

    $dataProvider->sort->attributes['statuscode.statusname'] = [
        'asc' => ['statuscode.statusname' => SORT_ASC],
        'desc' => ['statuscode.statusname' => SORT_DESC],
    ];

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    $query->joinWith(['client' => function($query) {
    $query->from(['client' => 'clients']); }]);
    $query->joinWith(['statuscode' => function($query) { $query->from(['statuscode' => 'statuses']); }]);

    // grid filtering conditions

    $query->andFilterWhere(['like', 'project.number', $this->number])
        ->andFilterWhere(['like', 'project.name', $this->name])
        ->andFilterWhere(['LIKE', 'client.name', $this->getAttribute('client.name')])
        ->andFilterWhere(['LIKE', 'statuscode.statusname', $this->getAttribute('statuscode.statusname')]);
    return $dataProvider;
}
}

1 个答案:

答案 0 :(得分:0)

没关系,我明白了! :)

return array_merge(parent::attributes(), ['client.name', 'statuscode.statusname']);

代替:

return array_merge(parent::attributes(), ['client.name']);
return array_merge(parent::attributes(), ['statuscode.statusname']);

LOL