【问题标题】:Eloquent ORM Dynamic Where - Is there a shortcut / more elegant solution?Eloquent ORM Dynamic Where - 有没有捷径/更优雅的解决方案?
【发布时间】:2013-09-05 14:55:42
【问题描述】:

下面的代码有更优雅/更好的解决方案吗?目前,我不得不重复很多查询,只是为了在查询中添加一个额外的“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();
    }

【问题讨论】:

标签: php orm laravel eloquent


【解决方案1】:

首先你为什么要$common == true

其次,您不需要一次完成所有构建,您可以这样做。

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

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

可以更好地格式化,但你明白了。

【讨论】:

  • 谢谢。 $common == true 只是为了说明问题而写的。
  • 我之前尝试过这种方法,但在链接时遇到了问题。我试图链接另一个 -> 当我已经使用 ->get() 方法时
【解决方案2】:

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

【讨论】:

  • $query = query->where('common', '=', 1);应该只是 $query->where('common', '=', 1);链接时不需要这样申请
【解决方案3】:

怎么样

$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();           
}

这样你就不需要 if 了。如果你必须不关心公共标志

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    相关资源
    最近更新 更多