如何为 Yii2 GridView 定义具有表名的限定属性

How to define qualify attribute with tablename for Yii2 GridView

我正在使用具有关系的 Yii2 GridView 模型。如果我尝试在索引页上通过 ID 进行搜索,则会导致 SQL 错误,因为该 ID 是明确的。

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous The SQL being executed was: SELECT COUNT(*) FROM re_ingredient LEFT JOIN re_ingredient_lang ON re_ingredient.id = re_ingredient_lang.ingredient_id LEFT JOIN re_ingredient_group_lang ON re_ingredient.ingredient_group_id = re_ingredient_group_lang.ingredient_group_id WHERE ((id='1') AND (re_ingredient_lang.language='en-US')) AND (re_ingredient_group_lang.language='en-US')

我的代码在views/ingredient/index.php:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        ['attribute' => 'id', 'contentOptions' => ['width' => '80px'] ],
        ['attribute' => 'NDB_No', 'contentOptions' => ['width' => '80px'] ],
        ['attribute' => 'group_name', 'value' => 'ingredientGroupLang.name'],
        ['attribute' => 'name', 'value' => 'translation.name'],
        ['attribute' => 'name_alt', 'value' => 'translation.name_alt'],
        'name_lat',
        // 'created_at',
        // 'updated_at',

        ['class' => 'yii\grid\ActionColumn', 
         'contentOptions' => ['style' => 'white-space: nowrap;'] ],
    ],
]); ?>

How can I achieve, that re_ingredient.id is used in the where clause instead of the simple id?

public function search()

中添加 re_ingredient.id 而不是 id
$query->andFilterWhere(['like', 're_ingredient.id', $this->id]) //here re_ingredient.id

就是这样。