gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:25:41 25 4
gpt4 key购买 nike

我对这个有点困惑。我在 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 工作台中,并将 ?s 替换为它们各自的值时,我得到了我期望的结果集。起初我认为日期不应该是字符串,但在工作台中,无论将它们作为字符串还是整数传递,我都得到了正确的结果集。我唯一的另一个想法是查询的参数数组中应该有 4 个值,每个值一个?在查询中,但我无法控制(至少据我所知。)

有人能看到我遗漏的东西吗?或者这是 Laravel 4 中的一个已知错误?

预先感谢您的帮助。

-埃里克

最佳答案

问题在于绑定(bind)的顺序:总是在 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();

为连接手动设置绑定(bind)也可能有助于强制绑定(bind)的顺序

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

这将确保您获得丢失的绑定(bind)值

关于php - Laravel-4 查询生成器未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com