There is a Category model, it is connected like this with three other models

public function getSerials(){ return $this->hasMany(Serial::className(),['id'=>'id_serial'])->viaTable('fl_cat_serial', ['id_cat' =>'id']); } public function getFilms(){ return $this->hasMany(Serial::className(),['id'=>'id_film'])->viaTable('fl_cat_serial', ['id_cat' =>'id']); } public function getMfilms(){ return $this->hasMany(Serial::className(),['id'=>'id_mfilm'])->viaTable('fl_cat_serial', ['id_cat' =>'id']); } 

there was no need to use three models together for the listveiw widget and shove it in the provider one I know you can use so

 $category= new Category; $serialDataProvider = new ActiveDataProvider([ 'query' =>$category->getSerials(), 'pagination'=>[ 'pageSize'=>60, ] ]); 

but how to use three at the same time I broke my head trying to solve it with the help of join like this

 $querys=CatSerial::find() ->select('fl_serial.id,fl_serial.name_serial,fl_serial.slug_serial,fl_serial.nesting,fl_film.id,fl_film.name_film,fl_film.slug_film,fl_film.nesting,fl_mfilm.id,fl_mfilm.name_mfilm,fl_mfilm.slug_mfilm,fl_mfilm.nesting') ->leftJoin('fl_serial','fl_cat_serial.id_serial = fl_serial.id') ->leftJoin('fl_film','fl_cat_serial.id_film = fl_film.id') ->leftJoin('fl_mfilm','fl_cat_serial.id_mfilm = fl_mfilm.id ') ->where(['id'=>$categoryThe->id]) ->groupBy('fl_cat_serial.id_serial, fl_cat_serial.id_film,fl_cat_serial.id_mfilm'); 

it gives me the following SQLSTATE error [23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

 The SQL being executed was: SELECT COUNT(*) FROM (SELECT `fl_serial`.`id`, `fl_serial`.`name_serial`, `fl_serial`.`slug_serial`, `fl_serial`.`nesting`, `fl_film`.`id`, `fl_film`.`name_film`, `fl_film`.`slug_film`, `fl_film`.`nesting`, `fl_mfilm`.`id`, `fl_mfilm`.`name_mfilm`, `fl_mfilm`.`slug_mfilm`, `fl_mfilm`.`nesting` FROM `fl_cat_serial` LEFT JOIN `fl_serial` ON fl_cat_serial.id_serial = fl_serial.id LEFT JOIN `fl_film` ON fl_cat_serial.id_film = fl_film.id LEFT JOIN `fl_mfilm` ON fl_cat_serial.id_mfilm = fl_mfilm.id WHERE `id`=1 GROUP BY `fl_cat_serial`.`id_serial`, `fl_cat_serial`.`id_film`, `fl_cat_serial`.`id_mfilm`) `c` 
  • Read about with. Back home in the evening will help if you do not succeed. rmcreative.ru/blog/post/yii2-join-vernulsja - Urmuz Tagizade
  • @UrmuzTagizade I corrected the answer there arrived on your advice but issued an error in the general look. - Sergalas
  • join is not necessary. Use connections in models through hasOne or hasMany depending on the task and link through with. For example: -> with (['serials']) - Urmuz Tagizade
  • And can you refer to this code for example? I don't understand honestly - Sergalas
  • I answer from the phone unfortunately. Houses will be only in the evening, after 9 - Urmuz Tagizade

1 answer 1

If I understood correctly

Can be done in a similar way:

 $ggCarrierRows = GgCarrierProfileRequest::find()->orderBy('id DESC')->all(); $wpCarrierRows = WpCarrierProfileRequest::find()->orderBy('id DESC')->all(); $ggCustomerRows = GgCustomerProfileRequest::find()->orderBy('id DESC')->all(); $wpCustomerRows = WpCustomerProfileRequest::find()->orderBy('id DESC')->all(); $dataProvider = new ArrayDataProvider([ 'allModels' => array_merge($ggCarrierRows, $wpCarrierRows, $ggCustomerRows, $wpCustomerRows), 'pagination' => [ 'pageSize' => 20, ], ]); return $this->render( 'index' , [ 'dataProvider' => $dataProvider, ]); 

True, there can be a problem if the fields do not overlap, and it can be problematic to display them in a GridView . However, this can be solved through a simple condition

 [ 'class' => 'yii\grid\ActionColumn', 'template' => '{view}', 'buttons' => [ 'view' => function ($url, $model) { if('common\models\GgCarrierProfileRequest' === get_class($model)) return \yii\helpers\Html::a('Посмотр', Url::to(['/profile-requests/gg-carrier-view', 'id' => $model->id])); if('common\models\WpCarrierProfileRequest' === get_class($model)) return \yii\helpers\Html::a('Посмотр', Url::to(['/profile-requests/wp-carrier-view', 'id' => $model->id])); if('common\models\GgCustomerProfileRequest' === get_class($model)) return \yii\helpers\Html::a('Посмотр', Url::to(['/profile-requests/gg-customer-view', 'id' => $model->id])); if('common\models\WpCustomerProfileRequest' === get_class($model)) return \yii\helpers\Html::a('Посмотр', Url::to(['/profile-requests/wp-customer-view', 'id' => $model->id])); }, ], ], 

This way you save yourself from complex queries and it is much easier to customize the table.