Getting unknown property

Hello everyone! Have an issue I cant solve a few days. I’m not so good in yii2, so I’ll really happy for some help.

I used GII and CRUD generator to make some files. That’s ok, works fine, 0 problems :)

Then I need to show in GridView (or in cycle like foreach nothing matter) some data from this model and from other db table (using join in query).

Here is search method my DeckSearch class (made by CRUD. I’m just edit $query):


 public function search($params)

    {

        $query = Deck::find()->select('d_id, d_author, d_name, d_heroid, d_desc, d_date, username')->join('LEFT JOIN', 'user', 'deck.d_author=user.id')->orderBy(['d_date' => SORT_DESC]);

        

        // add conditions that should always apply here


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        $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;

        }


        // grid filtering conditions

        $query->andFilterWhere([

            //'d_id' => $this->d_id,

            'd_author' => $this->d_author,

            //'d_date' => $this->d_date,

        ]);


        $query->andFilterWhere(['like', 'd_heroid', $this->d_heroid]);

            //->andFilterWhere(['like', 'd_heroname', $this->d_heroname])

            //->andFilterWhere(['like', 'd_name', $this->d_name])

            //->andFilterWhere(['like', 'd_desc', $this->d_desc])

            //->andFilterWhere(['like', 'd_deck', $this->d_deck]);


        return $dataProvider;

    }

When i trying to show data from any joined table in view I everytime get error like:


Unknown Property – yii\base\UnknownPropertyException


Getting unknown property: app\models\Deck::username

Help me please. How can i make to join some data from other table/tables and show it on view?

Since,username is not a field defined in Deck model. You can’t use it. You can do this

In Dec Model add a method like this

public function getUserName()


{


    return $this->hasOne(User::className(), ['id' => 'd_author']);


}

Then in the view you can use ‘userName.username’,

I this this is the best approach - http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/