Cakephp3降低了SQL查询的范围

时间:2018-06-07 11:48:49

标签: mysql cakephp

我遇到了以下SQL语句片段的问题:

...->where([
        'Vlans.id' => $this->id,
        'AND' => [
            ["TIMESTAMPDIFF(DAY, Requests.timestamp, CURDATE()) <" => $this->days]
        ]
    ])...

Cakephp降低了一些部分(requests.timestamp和curdate):

(Vlans.id = :c0 AND TIMESTAMPDIFF(DAY, requests.timestamp, curdate()) < :c1)

降低CURDATE()的问题并不是一个问题,因为它是一个SQL函数。 但我将requests-table别名化为Requests(大写R)。

有没有办法阻止小写或其他解决方案,而不是改变我的所有别名?

谢谢!

1 个答案:

答案 0 :(得分:2)

你应该使用cake SQL函数

$f = $query->func()->timestampdiff([
    'DAY' => 'literal', 
    'Requests.timestamp' => 'identifier', 
    $query->func()->curdate()
]);
$days = $this->days;
$query->where(function ($exp) use ($f,  $days) {
    return $exp->lt($f, $days);
});

请参阅手册here

相关问题