雄辩的ORM动态在哪里 - 是否有快捷/更优雅的解决方案?

时间:2013-09-05 14:55:43

标签: php orm laravel eloquent

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

3 个答案:

答案 0 :(得分:4)

首先,你为什么要做$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();

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

答案 1 :(得分:0)

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

答案 2 :(得分:-2)

怎么样

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