There are 3 tables ( order , order_delivery , order_types ). For all created models for order gridview. In the order table there is an order_number column, in the order_delivery table, the order_delivery and order_delivery_code , in the order_types table in the delivery_types_code and delivery_types_name columns. I can create a function in the Order model. Function public function getType(){ return $this->hasOne(OrderDelivery::className(), ['order_id' => 'order_id']); } public function getType(){ return $this->hasOne(OrderDelivery::className(), ['order_id' => 'order_id']); } in the 'type.order_delivery_code' index and output the delivery codes from order_delivery . The question is how, instead of delivery codes, to output the decryption from the 3 table delivery_types_name ?

Upd. Did so.

public function getType(){ return $this->hasOne(DeliveryTypes::className(), ['delivery-types_code' => 'order_delivery_code']) ->viaTable('order_delivery', ['order_id' => 'order_id']); } public function getType(){ return $this->hasOne(DeliveryTypes::className(), ['delivery-types_code' => 'order_delivery_code']) ->viaTable('order_delivery', ['order_id' => 'order_id']); } Thank you all for participating.

  • I think the question is very similar to this: ru.stackoverflow.com/questions/520759 - MasterAlex
  • @MasterAlex please clarify your answer below - cruim
  • @MasterAlex for the second day I’m fighting on the solution of the problem (below). Do you mean create a variable in the Order or OrderSearch ? show approximate code please. - cruim

2 answers 2

Not much is not clear what the problem is, I want to find some kind of native solution so as not to join labels? Well, as an option in each model to create a static method that will receive an input id and an output decryption. But I would probably bother, but just made a method in which I would override the required. something like this

The widget itself:

 <?= GridView::widget([ 'dataProvider' => $model->getOrderDelivery(), 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'orderNumber' , 'deliveryTypesName' ] ]); ?> 

Sampling method and dataProvider generation

 public function getOrderDelivery(){ //Получим поля , свяжем с типом $query = new Query(); $query->select(['O.order_number orderNumber' , 'OT.delivery_types_name deliveryTypesName']) ->from(['order O']) ->leftJoin('order_delivery OD', 'OD.order_number = O.order_number') ->leftJoin('order_types OT', 'OT.order_delivery_code = OD.order_delivery_code '); //получим provider для GridView $provider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 50, ], 'sort' => ['attributes' => ['orderNumber', 'deliveryTypesName']], ]); return $provider; } 

    This issue is easily solved through relations :

    Model Order.php

     public function getDelivery(){ return $this->hasOne(OrderDelivery::className(), ['order_number' => 'order_number']); } 

    Model OrderDelivery.php

     public function getType(){ return $this->hasOne(OrderTypes::className(), ['delivery_types_code' => 'order_delivery_code']); } 

    Further, when loading the Order model in view, you can get delivery_types_name using this variable:

     $model->delivery->type->delivery_types_name 
    • I apologize for the noob question, $model->delivery->type->delivery_types_name in 'columns' => [ ? - cruim
    • @cruim, no, this output will not work through the GridView, for this you need to create a separate field in the model, to which you assign the value - MasterAlex