问题描述
我需要比较DATETIME中的DATE格式.
I need to compare DATE format from a DATETIME.
在ZF 2.3.5之前的版本中,以下代码可以正常工作:
Prior ZF 2.3.5, the following code was working fine:
$select->where('DATE(arrival_date) <= DATE(NOW())');
$select->where('DATE(departure_date) >= DATE(NOW())');
$select->where('enable = true');
在ZF 2.4.2+中,它不再起作用,并产生以下错误:
With ZF 2.4.2+ it does not work anymore and produce the following error:
无法从Zend \ Db \ Sql \ Predicate \ PredicateInterface接口继承或继承常量TYPE_IDENTIFIER
我尝试了以下操作(无错误).问题是"arrival_date"是DATETIME格式,我只需要与DATE进行比较.
I have tried the following (without error). The problem is that "arrival_date" is a DATETIME format, and I need to compare with a DATE only.
$where = new Zend\Db\Sql\Where();
$where->lessThanOrEqualTo('arrival_date', date('Y-m-d'));
$where->greaterThanOrEqualTo('arrival_date', date('Y-m-d'));
$where->equalTo('enable', true);
$select->where($where);
理想情况下,此代码应该可以工作,但不能:
Ideally, this code should work, but it does not:
$select->where(new Zend\Db\Sql\Predicate\Expression('DATE(arrival_time) <= ?', 'DATE(NOW())'));
我迷路了,知道吗?
推荐答案
只需使用像这样的表达式:
Just use Expression like this:
$select->where(
array(
new \Zend\Db\Sql\Predicate\Expression("DATE(arrival_date) <= DATE(NOW())")
)
);
不需要?就像你尝试过的一样.和 ?您的参数将被转义,并且不能与表达式一起使用.仅适用于参数,例如PHP datetime或类似的东西.
No need of ? like you tried. With ? you params will be escaped and it won't work with an expression. Only with params, like a PHP datetime or something like this.
经过2.4.2测试
这篇关于PHP ZF2使用SQL函数选择where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!