我正在使用与Yii2 GridView有关系的模型。如果我尝试通过索引页面上的ID进行搜索,则会导致SQL错误,因为该ID是明确的。
SQLSTATE [23000]:完整性约束违规:1052列'id' where子句不明确正在执行的SQL是:SELECT COUNT(*) 来自
re_ingredient
LEFT JOINre_ingredient_lang
ONre_ingredient
。id
=re_ingredient_lang
。ingredient_id
LEFT JOINre_ingredient_group_lang
re_ingredient
。ingredient_group_id
=re_ingredient_group_lang
。ingredient_group_id
WHERE((id
='1')和 (re_ingredient_lang
。language ='en-US'))AND (re_ingredient_group_lang
。语言= '的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;'] ], ], ]); ?>
我怎样才能实现,
re_ingredient.id
用于where子句而不是简单id
?
答案 0 :(得分:0)
在re_ingredient.id
id
而不是public function search()
$query->andFilterWhere(['like', 're_ingredient.id', $this->id]) //here re_ingredient.id
是的。