如何使用Yii2 GridView的表名定义质量属性

时间:2015-02-06 08:58:59

标签: gridview yii2 relation

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

  

SQLSTATE [23000]:完整性约束违规:1052列'id'   where子句不明确正在执行的SQL是:SELECT COUNT(*)   来自re_ingredient LEFT JOIN re_ingredient_lang ON   re_ingredientid = re_ingredient_langingredient_id LEFT JOIN   re_ingredient_group_lang re_ingredientingredient_group_id =   re_ingredient_group_langingredient_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

1 个答案:

答案 0 :(得分:0)

re_ingredient.id

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

是的。