首页 > 解决方案 > 这是检查该值是否为空的最有效方法吗?

问题描述

我正在对我们的费率表进行查询,如您所见。如果值返回为空。它再次检查并使用不同的用例一遍又一遍地执行此操作。我只是想确保这是最有效的检查方式。

$rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '$branch' AND location_from = '$location_from_id' AND location_to = '$location_to_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();        
        
if(empty($rates))
{
    //IF THERE IS MASTER, NO BRANCH, YES LOCATION
    $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '-1' AND location_from = '$location_from_id' AND location_to = '$location_to_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
    if($region_from_id !='' && $region_to_id !='')
    {
                
        if(empty($rates)) { // IF RESTRICTED IS PICKUP
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '$branch' AND region_from = '$region_from_id' AND region_to =   '$region_to_id' AND restricted = '1' AND restricted_location_id = '$location_from_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
        if(empty($rates)) { // IF RESTRICTED IS DELIVERY
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '$branch' AND region_from = '$region_from_id' AND region_to =   '$region_to_id' AND restricted = '2' AND restricted_location_id = '$location_to_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
        if(empty($rates)) {
            // IF THERE IS NO RESTRICTION
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '$branch' AND region_from = '$region_from_id' AND region_to =   '$region_to_id' AND restricted = '0' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }

        if(empty($rates)) { // IF RESTRICTED IS PICKUP
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '-1' AND region_from = '$region_from_id' AND region_to = '$region_to_id' AND restricted = '1' AND restricted_location_id = '$location_from_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
        if(empty($rates)) { // IF RESTRICTED IS DELIVERY
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '-1' AND region_from = '$region_from_id' AND region_to = '$region_to_id' AND restricted = '2' AND restricted_location_id = '$location_to_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
        if(empty($rates)) {
            // IF THERE IS NO RESTRICTION
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '$account' AND branch_id = '-1' AND region_from = '$region_from_id' AND region_to = '$region_to_id' AND restricted = '0' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }

        if(empty($rates)) { // IF RESTRICTED IS PICKUP
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '-1' AND branch_id = '-1' AND region_from = '$region_from_id' AND region_to = '$region_to_id' AND restricted = '1' AND restricted_location_id = '$location_from_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
        if(empty($rates)) { // IF RESTRICTED IS DELIVERY
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '-1' AND branch_id = '-1' AND region_from = '$region_from_id' AND region_to = '$region_to_id' AND restricted = '2' AND restricted_location_id = '$location_to_id' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
        if(empty($rates)) {
            // IF THERE IS NO RESTRICTION
            $rates = Rates::find()->where("status_active = 'active' AND master_account = '-1' AND branch_id = '-1' AND region_from = '$region_from_id' AND region_to = '$region_to_id' AND restricted = '0' AND CAST('$order_placed_time' AS time) BETWEEN book_from AND book_by")->all();
        }
    }
}

标签: phpmysql

解决方案


推荐阅读