我有主表和几个子表。yii2连接表中的GridView
主表产品:的productID/productNameID/productColorID
和子表
产品名称:productNameID /名称
productColor:productColorID /名称
在主表中,我只是插入子表的ID。 并获得正常的名称,而不是我的ID使用功能的产品型号:
public function getProductName()
{
return $this->hasOne(ProductName::className(), ['nameID' => 'productNameID']);
}
public function getProductColor()
{
return $this->hasOne(ProductColor::className(), ['colorID' => 'productColorID']);
}
如果我在视图中使用的唯一模式,我可以写$model->productName['name']
从子表得到的名字。
但我想创建GridView控件。为此我从Gii创建了默认的CRUD。正如你所知道的GridView使用SearchModel。 当我在列表中做到这一点时,我只有主表中的ID。可能是因为SearchModel中没有自定义函数 我的意思是现在没有连接存储名称的子表的连接。 那么如何将我的主表连接到GridView中的子表?应该怎样做才能做到这一点?