[英]Yii2 Filter data with multiple relation
我正在努力並列出船只有兩種類型的船只頁面
如果有共用船只
我得檢查一下
如果沒有共用船
對於這些定價我使用的條件,但在過濾從高到低/從低到高的價格過濾我不知道如何應用條件。 有人可以幫我解決這些問題。
楷模 :
Boatinformation(Main)
。
Boatsharedfishingtrip(shared pricing)
Boatfishingcharter(Private Pricing)
模型關系:
public function getBoatfishingcharter() {
return $this->hasOne(Boatfishingcharter::className(), ['boat_id' => 'boat_id']);
}
public function getBoatsharedtrip() {
return $this->hasOne(Boatsharedfishingtrip::className(), ['boat_id' => 'boat_id']);
}
if($model->boatsharedtrip->shared_fishing_charters == '1') {
if($model->boatsharedtrip->one_two_day_4hrs == 1) {
$shared_price = $model->boatsharedtrip->one_two_day_rate_per_angler;
} else if($model->boatsharedtrip->three_four_day_6hrs == 1) {
$shared_price = $model->boatsharedtrip->three_four_day_rate_per_angler;
} else if($model->boatsharedtrip->full_day_8hrs == 1) {
$shared_price = $model->boatsharedtrip->full_day_rate_per_angler;
}
} else if($model->boatfishingcharter->private_fishing_charters == 1) {
if($model->boatfishingcharter->one_two_day_4hrs == 1) {
$private_price = $model->boatfishingcharter->one_two_day_rate;
} else if($model->boatfishingcharter->three_four_day_6hrs == 1) {
$private_price = $model->boatfishingcharter->three_four_day_rate;
} else if($model->boatfishingcharter->full_day_8hrs == 1) {
$private_price = $model->boatfishingcharter->full_day_rate;
}
}
對於我目前使用的過濾器:
if($_GET['orderby'] == 'price_desc') {
$query->orderBy(['one_two_day_rate_per_angler' => SORT_DESC, 'one_two_day_rate' => SORT_DESC, 'three_four_day_rate_per_angler' => SORT_DESC, 'three_four_day_rate' => SORT_DESC, 'full_day_rate_per_angler' => SORT_DESC, 'full_day_rate' => SORT_DESC]);
} elseif($_GET['orderby'] == 'price_asc') {
$query->orderBy(['one_two_day_rate_per_angler' => SORT_ASC, 'one_two_day_rate' => SORT_ASC, 'three_four_day_rate_per_angler' => SORT_ASC, 'three_four_day_rate' => SORT_ASC, 'full_day_rate_per_angler' => SORT_ASC, 'full_day_rate' => SORT_ASC]);
}
$query = Boatinformation::find();
$query->leftJoin('boatsharedfishingtrip', '`boatsharedfishingtrip`.`boat_id` = `boatinformation`.`boat_id`')
->leftJoin('rating_review', '`rating_review`.`boat_id` = `boatinformation`.`boat_id`')
->leftJoin('boatotherservice', '`boatotherservice`.`boat_id` = `boatinformation`.`boat_id`')
->leftJoin('boatfishingequipment', '`boatfishingequipment`.`boat_id` = `boatinformation`.`boat_id`')
->join('INNER JOIN','boatcompanyinformation', '`boatcompanyinformation`.`id` = `boatinformation`.`boat_id`')
->leftJoin('boatfishingcharter', '`boatfishingcharter`.`boat_id` = `boatinformation`.`boat_id`')
->where(['boat_publish' => 1])->all();
編輯
這就是我在列表中展示船只的方式
我是否正確理解表之間的關系類型是“一對一”? 我的意思是Boatinformation,Boatsharedfishingtrip和Boatfishingcharter?
嘗試這個。
// Main query
$query = Boatinformation::find()
->leftJoin('boatsharedfishingtrip', '`boatsharedfishingtrip`.`boat_id` = `boatinformation`.`boat_id`')
->leftJoin('rating_review', '`rating_review`.`boat_id` = `boatinformation`.`boat_id`')
->leftJoin('boatotherservice', '`boatotherservice`.`boat_id` = `boatinformation`.`boat_id`')
->leftJoin('boatfishingequipment', '`boatfishingequipment`.`boat_id` = `boatinformation`.`boat_id`')
->join('INNER JOIN', 'boatcompanyinformation', '`boatcompanyinformation`.`id` = `boatinformation`.`boat_id`')
->leftJoin('boatfishingcharter', '`boatfishingcharter`.`boat_id` = `boatinformation`.`boat_id`')
->where(['boat_publish' => 1]);
// Sort
$orderBy = Yii::$app->request->get('orderby');
switch ($orderBy) {
case 'price_desc':
$query->orderBy(['one_two_day_rate_per_angler' => SORT_DESC, 'one_two_day_rate' => SORT_DESC, 'three_four_day_rate_per_angler' => SORT_DESC, 'three_four_day_rate' => SORT_DESC, 'full_day_rate_per_angler' => SORT_DESC, 'full_day_rate' => SORT_DESC]);
break;
case 'price_asc':
$query->orderBy(['one_two_day_rate_per_angler' => SORT_ASC, 'one_two_day_rate' => SORT_ASC, 'three_four_day_rate_per_angler' => SORT_ASC, 'three_four_day_rate' => SORT_ASC, 'full_day_rate_per_angler' => SORT_ASC, 'full_day_rate' => SORT_ASC]);
break;
}
// Execute
// var_dump( $query->createCommand()->getRawSql() ); // Uncomment to debug built SQL
$query->all();
您使用的是Yii2調試工具欄嗎? 如果是這樣,復制粘貼這里構建SQL。 如果不是,請取消注釋getRawSql
行。 它可能有助於弄清楚它為什么不適合你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.