简体   繁体   English

雄辩的ORM动态何处-是否有捷径/更优雅的解决方案?

[英]Eloquent ORM Dynamic Where - Is there a shortcut / more elegant solution?

Is there a more elegant / better solution to the below code? 下面的代码是否有更优雅/更好的解决方案? At present, I have to repeat a lot of query just to add an extra 'where' into the query. 目前,我不得不重复很多查询,只是要在查询中添加一个额外的“ where”。

    if ($common == true) {
        $products = self::with(array(
               'metal', 
               'metal.fixes.currency', 
               'metal.fixes' => function($query) use ($currency_id){
                   $query->where('currency_id', '=', $currency_id);
               }))
           ->where('metal_id', '=', $metal_id)
           ->where('product_type_id', '=', $product_type_id)
           ->where('common', '=', 1) // This line is the only difference 
                                            between the two Queries
           ->get();           
    }
    else {
        $products = self::with(array(
            'metal', 
            'metal.fixes.currency', 
            'metal.fixes' => function($query) use ($currency_id){
                $query->where('currency_id', '=', $currency_id);
            }))
        ->where('metal_id', '=', $metal_id)
        ->where('product_type_id', '=', $product_type_id)
        ->get();
    }

Firstly why are you doing $common == true ? 首先,为什么要执行$common == true

Secondly you don't need to do all building at once, here is how you can do it. 其次,您不需要一次完成所有构建,这是您可以做到的。

PHP 5.3 PHP 5.3

$products = $this->with(
     array('metal', 
           'metal.fixes.currency', 
           'metal.fixes' => function($query) use ($currency_id)
           {
                $query->where('currency_id', '=', $currency_id);
           }))
          ->where('metal_id', $metal_id)
          ->where('product_type_id', $product_type_id);

if ($common)
{
    $products->where('common', 1);
}

$products = $products->get();

PHP 5.4 PHP 5.4

$products = $this->with(
          ['metal', 
           'metal.fixes.currency', 
           'metal.fixes' => function($query) use ($currency_id)
           {
                $query->where('currency_id', '=', $currency_id);
           }])
          ->where('metal_id', $metal_id)
          ->where('product_type_id', $product_type_id);

if ($common)
{
    $products->where('common', 1);
}

$products = $products->get();

Could be formatted better but you get the idea. 可以格式化得更好,但是您知道了。

Sinque Eloquent/QueryBuilder always return a reference to itself you can write a more elegant version as follow: Sinque Eloquent / QueryBuilder总是返回对自身的引用,您可以编写一个更优雅的版本,如下所示:

$query = self::with(array(
               'metal', 
               'metal.fixes.currency', 
               'metal.fixes' => function($query) use ($currency_id){
                   $query->where('currency_id', $currency_id);
               }))
           ->where('metal_id', $metal_id)
           ->where('product_type_id', $product_type_id)

if ($common) {
    $query = query->where('common', 1);        
}

$products = $query->get();

How about 怎么样

$products = self::with(array(
               'metal', 
               'metal.fixes.currency', 
               'metal.fixes' => function($query) use ($currency_id){
                   $query->where('currency_id', '=', $currency_id);
               }))
           ->where('metal_id', '=', $metal_id)
           ->where('product_type_id', '=', $product_type_id)
           ->where('common', '=', (int)$common) // This line is the only difference 
                                            between the two Queries
           ->get();           
}

That way you do not need the if. 这样,您就不需要if。 If you must you do not care about the common flag 如果您一定不关心通用标志

$products = self::with(array(
               'metal', 
               'metal.fixes.currency', 
               'metal.fixes' => function($query) use ($currency_id){
                   $query->where('currency_id', '=', $currency_id);
               }))
           ->where('metal_id', '=', $metal_id)
           ->where('product_type_id', '=', $product_type_id);

$products = ($common) ? $products->where('common', 1)->get() : $products->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM