Yii2过滤具有多重关系的数据

时间:2018-08-31 07:50:18

标签: sorting yii yii2 filtering yii2-advanced-app

我正在研究并列出两种类型的船

  • 共享。
  • 私人。

如果有共享船可用

我必须检查

  • 如果他们提供半天(1/2)行程,则会显示半天的价格。
  • 如果没有半天时间,请检查一天的四分之三(3/4),价格是否为一天的四分之三。
  • 否则要全天检查并收取全天价格。

如果共享船不可用

  • 检查是否有私人船只。
  • 然后将上述情况应用于私人船只。

对于这些定价,我使用了条件,但是在过滤高到低/低到高价格过滤时,我不知道如何应用条件。有人可以帮我解决这些问题吗?

型号:

  • 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();

修改

这就是我在列表中显示船只的方式

enter image description here

1 个答案:

答案 0 :(得分:0)

我是否正确理解表之间的关系类型是“一对一”? 我的意思是划船信息,划船共享钓鱼之旅和划船宪章?

尝试一下。

// 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行。可能有助于弄清为什么它对您不起作用。