Laravel-4查询生成器未返回预期结果

时间:2014-03-17 16:33:29

标签: php mysql laravel-4

我对这个感到有点困惑。我已经在laravel中构建了一个查询,正如我所期望的那样被解释,但是当它不应该返回时,它返回一个空的结果集。

这是我的laravel代码:

$results = Outstanding::where('Outstanding.creditor_id', '=', $creditor->id)
    ->whereBetween('Outstanding.yyyymm', array($this->last_yyyymm, $this->now))
    ->select('Outstanding.yyyymm',
        'Outstanding.deviation_30',
        'Outstanding.deviation_60',
        'Outstanding.deviation_90',
        'Outstanding.deviation_over_90',
        'Outstanding.pay_amt',
        'Outstanding.outstanding',
        'Monthly_Historical.inv_on_hold_sum')
    ->leftJoin('Monthly_Historical', function($join) use($creditor){
        $join->on('Outstanding.yyyymm', '=', 'Monthly_Historical.yyyymm')
        ->where('Monthly_Historical.creditor_id', '=', $creditor->id);
    })
    ->groupBy('Outstanding.yyyymm')
        ->get();

当我查看有关此问题的查询时,我会看到以下内容:

select `Outstanding`.`yyyymm`, `Outstanding`.`deviation_30`, 
`Outstanding`.`deviation_60`, `Outstanding`.`deviation_90`, 
`Outstanding`.`deviation_over_90`, `Outstanding`.`pay_amt`, 
`Outstanding`.`outstanding`, `Monthly_Historical`.`inv_on_hold_sum` 
from `Outstanding` left join `Monthly_Historical` on 
`Outstanding`.`yyyymm` = `Monthly_Historical`.`yyyymm` and 
`Monthly_Historical`.`creditor_id` = ? where `Outstanding`.`creditor_id` = ? and 
`Outstanding`.`yyyymm` between ? and ? group by `Outstanding`.`yyyymm`
array(3) {
  [0]=>
  int(2)
  [1]=>
  string(6) "201301"
  [2]=>
  string(6) "201401"

当我将其复制/粘贴到MySql工作台中,并用各自的值替换?时,我得到了我期望的结果集。起初我认为日期不应该是字符串,但在工作台中我得到了正确的结果集,无论将它们作为字符串或整数传递。我唯一的另一个想法是查询的参数数组中应该有4个值,每个值一个?在查询中,但我无法控制(至少据我所知。)

任何人都能看到我失踪的东西吗?或者这是Laravel 4中的一个已知错误?

提前感谢您的帮助。

-Eric

1 个答案:

答案 0 :(得分:2)

问题是绑定的顺序:总是在WHERE子句之前执行JOIN

$results = Outstanding::select('Outstanding.yyyymm',
        'Outstanding.deviation_30',
        'Outstanding.deviation_60',
        'Outstanding.deviation_90',
        'Outstanding.deviation_over_90',
        'Outstanding.pay_amt',
        'Outstanding.outstanding',
        'Monthly_Historical.inv_on_hold_sum')
    ->leftJoin('Monthly_Historical', function($join) use($creditor){
        $join->on('Outstanding.yyyymm', '=', 'Monthly_Historical.yyyymm')
        ->where('Monthly_Historical.creditor_id', '=', $creditor->id);
    })
    ->where('Outstanding.creditor_id', '=', $creditor->id)
    ->whereBetween('Outstanding.yyyymm', array($this->last_yyyymm, $this->now))
    ->groupBy('Outstanding.yyyymm')
        ->get();

手动设置连接的绑定也可能有助于强制绑定的排序

$results = Outstanding::select(
    'Outstanding.yyyymm',
    'Outstanding.deviation_30',
    'Outstanding.deviation_60',
    'Outstanding.deviation_90',
    'Outstanding.deviation_over_90',
    'Outstanding.pay_amt',
    'Outstanding.outstanding',
    'Monthly_Historical.inv_on_hold_sum'
)->leftJoin(
    'Monthly_Historical', 
    function($join) use($creditor) {
        $join->on('Outstanding.yyyymm', '=', 'Monthly_Historical.yyyymm')
        ->on('Monthly_Historical.creditor_id', '=', DB::raw('?'));
    })
    ->setBindings(
        array_merge(
            $this->query->getBindings(),
            array($creditor->id)
        )
    )
    ->where('Outstanding.creditor_id', '=', $creditor->id)
    ->whereBetween('Outstanding.yyyymm', array($this->last_yyyymm, $this->now))
    ->groupBy('Outstanding.yyyymm')
        ->get();

这将确保您获得缺少的绑定值