模型中的关系表yii2

时间:2016-12-21 13:52:48

标签: php yii2

我有3个表格(orderorder_deliveryorder_types)。每张桌子都有模特。对于order,我创建了gridview。表格order包含列order_number,表order_delivery包含order_numberorder_delivery_code,表格order_type delivery_types_codedelivery_types_name 。按顺序我写函数public function getType(){ return $this->hasOne(OrderDelivery::className(), ['order_id' => 'order_id']); }并在索引中写'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'type.order_delivery_code'。并获取gridview中订单的order_delivery_code。对于gridview中的订单,如何从order_type获取delivery_types_name?

1 个答案:

答案 0 :(得分:0)

订单模型中的

创建与OrderDelivery的关系

OrderDelivery模型中的

创建与OrderDeliveryType

的关系 SearchOrder中的

是这样的:

$query->with(['orderDelivery','orderDelivery.orderDeliveryType'])

并在index.php中使用GridView:

'attribute'=>'orderDelivery.orderDeliverType.delivery_types_name'