我正在研究船只的列表页面,其中船只有两种类型
- 已共享。
- 不公开。
如果有共享船只
我必须检查一下
- 如果他们提供半日 (1/2) 行程,则将显示半日的价格。
- 如果半天不可用,则检查是否为四分之三 (3/4) 天,价格将为四分之三天。
- 否则检查全天并获取全天价格。
如果没有共享船只
- 检查是否有可用的私有(private)船只。
- 然后对私有(private)船只应用上面给出的相同场景。
对于这些定价,我使用了条件,但在从高到低/从低到高的价格过滤中,我不知道如何应用条件。有人可以帮我解决这些问题吗?
型号:
船舶信息(主)
.Boatsharedfishingtrip(共享定价)
船钓包船(私有(private)定价)
模型关系:
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();
编辑
这就是我在列表中显示船只的方式
最佳答案
我是否正确理解表之间的关系类型是“一对一”? 我的意思是船信息、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
行。它可能有助于找出为什么它不适合您。
关于sorting - Yii2 过滤具有多重关系的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52110864/