When linking news with categories, the search by id and title stopped in the GridView. Gives an error message:

SQLSTATE [23000]: Integrity constraint violation: 1052 Column 'id' in clause is the ambiguous category = 123_news_category . id WHERE id = '2'

In the model:

 public function getNewsCategory() { return News::hasOne(NewsCategory::className(), ['id' => 'category']); } 

In search:

 $query->joinWith('newsCategory'); 

In the gridview:

 'category' => [ 'attribute' => 'category', 'value' => function($model){ return $model->newsCategory['title']; }, 'filter' => NewsCategory::allNewsCategory(), ], 

But searching by categories, views and status works.

    1 answer 1

    Everything turned out to be much easier. In model:

     public function getNewsCategory() { return $this->hasOne(NewsCategory::className(), ['id' => 'category']); } public function getNewsCategoryTitle() { return $this->newsCategory->title; } 

    In search:

     public $newsCategoryTitle; 

    In the gridview:

      'category' => [ 'attribute' => 'category', 'value' => 'newsCategoryTitle', 'filter' => NewsCategory::allNewsCategory(), ],