查询中的cakephp 3.x datetime字段比较未返回正确结果

查询中的cakephp 3.x datetime字段比较未返回正确结果,php,mysql,cakephp,query-builder,cakephp-3.x,Php,Mysql,Cakephp,Query Builder,Cakephp 3.x,尝试获取24小时或之前创建的所有记录。这里的“created”是一个datatime字段。DB中有三条记录必须满足此条件,但返回0条记录 $my_table_tbl = TableRegistry::get('my_table'); $records = $my_table_tbl ->find() ->where([ 'created <' => '(NOW() - INTERVAL 1 DAY)', 'status' =

尝试获取24小时或之前创建的所有记录。这里的“created”是一个datatime字段。DB中有三条记录必须满足此条件,但返回0条记录

$my_table_tbl = TableRegistry::get('my_table');
$records = $my_table_tbl
    ->find()
    ->where([
        'created <' => '(NOW() - INTERVAL 1 DAY)',
        'status' => 'pending'
    ])
    ->toArray();
$my_table_tbl=TableRegistry::get('my_table');
$records=$my\u table\u tbl
->查找()
->在哪里([

'createdCakePHP将始终将子句的右侧视为变量绑定。这意味着
(NOW()-INTERVAL 1 DAY)
将被解释为字符串值

您可以通过将查询转换为字符串来检查生成了什么SQL

$query = $my_table_tbl->find()
              ->where([
                 'created <' => '(NOW() - INTERVAL 1 DAY)',
                 'status' => 'pending'
              ]);
dd((string)$query);
$query=$my\u table\u tbl->find()
->在哪里([

'为了完整性而创建,还有一个支持生成跨架构兼容SQL的方法。@cgtag它对我有用,非常感谢。newExpr()-创建行表达式的方法发挥了神奇的作用。
$q = $my_table_tbl->query();
$query = $my_table_tbl
    ->find()
    ->where([
        'created <' => $q->newExp('(NOW() - INTERVAL 1 DAY)'),
        'status' => 'pending'
    ]);
dd((string)$query);