簡體   English   中英

Yii2過濾具有多個關系的數據

[英]Yii2 Filter data with multiple relation

我正在努力並列出船只有兩種類型的船只頁面

  • 共享。
  • 私人的。

如果有共用船只

我得檢查一下

  • 如果他們提供半天(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();

編輯

這就是我在列表中展示船只的方式

在此輸入圖像描述

我是否正確理解表之間的關系類型是“一對一”? 我的意思是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM