Неизвестное свойство - yii \ base \ UnknownPropertyException - PullRequest
0 голосов
/ 28 мая 2019

Я получил ошибку в заголовке при попытке отобразить реляционное поле в gridview.У меня проблема с 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;
}
}

Мой взгляд:

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;

$this->title = 'Projects';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="project-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p><?= Html::a('Create Project', ['create'], ['class' => 'btn btn-success']) ?></p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'number',
'client.name',
'name',
    [
    'attribute' => 'statuscode.statusname',
    'label' => 'Projectstatus',
    ],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>

1 Ответ

0 голосов
/ 28 мая 2019

Неважно, я понял! :)

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

вместо:

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

LOL

...