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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2过滤具有多个关系的数据相关的知识,希望对你有一定的参考价值。

我正在努力并列出船只有两种类型的船只页面

  • 共享。
  • 私人的。

如果有共用船只

我得检查一下

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

答案

我是否正确理解表之间的关系类型是“一对一”?我的意思是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线。它可能有助于弄清楚它为什么不适合你。

以上是关于Yii2过滤具有多个关系的数据的主要内容,如果未能解决你的问题,请参考以下文章

Yii2 和 reactjs CORS 过滤器给出错误:预检响应具有无效的 HTTP 状态代码 401

Yii2 gridview过滤来自多个值的列表(不是下拉列表过滤器)

是否有一个 NSPredicate 来过滤具有一对多关系的模型(过滤多个)?

Yii2:在多个表之间定义关系的正确方法

Yii2过滤网格视图,带有多个$ _GET参数

具有三层深度嵌套模型的查询集过滤器(多个一对多关系)