Yii2 - left join on multiple condition

I have three tables with the following relations.

  ------- 1        0..* ------------
 |Product|-------------|Availability|
  -------               ------------
    1 |
      |
    1 |
  --------
 |MetaData|
  --------
$products = Product::find()
->joinWith(['metaData' => function (ActiveQuery $query) {
return $query
->andWhere(['=', 'meta_data.published_state', 1]);
}])
->joinWith(['availability' => function (ActiveQuery $query) {
return $query
->andOnCondition(['>=', 'availability.start', strtotime('+7 days')])
->andWhere(['IS', 'availability.ID', NULL]);
}])
->all();
----------------------------------------------------------------------
$articalPost = ArticalMaster::find()->joinWith(['media' => function () {}])->all();
$allPost = [];
foreach ($articalPost as $post){
$image = [];
foreach ($post->media as $media){
$image[] = $media->vImage;
}
$post = $post->toArray();
$post['images'] = $image;
$allPost[] = array(
'post' => $post,
);
}

echo"<pre>";print_r($allPost);

Index Page in display another Model data

use yii\helpers\Url;
[
'label' => 'Image',
'format'=>'html',
'value' => function($model) {
if (count($model->media) != 0){
foreach ($model->media as $file) {
$imagePath = Url::base() . '/web/image/' . $file['vImage'];
}
}
return Html::img($imagePath, ['width'=>'60','height'=>'60']);
},
],

ArticalMaster Model

public function getMedia()
{
/*
* Assumptions:
* - foreign key named iPostId
* - primary key named iArticalId
*/
return $this->hasMany(ArticalImage::className(), ['iPostId' => 'iArticalId']);
}

Popular posts from this blog

Ajax Toggle(on/off) on-click Update

Yii Framework In Update Time View Image In Form