如何在zend框架2中构建复杂的删除查询

时间:2013-12-01 00:48:38

标签: php zend-framework2

我有一个类,它是Zend TableGateway的一个实例,我正在尝试构建以下删除查询(其中:start,:channel和:stop是变量)

 DELETE FROM epg WHERE (epg_start >= '2013-11-30 11:00:00') and FROM_UNIXTIME(UNIX_TIMESTAMP(epg_start) + epg_duration) <= '2013-12-01 01:30:00') AND epg_service = 'skytv' AND epg_channel = '4music'

这是我到目前为止所尝试的内容

$where = new Where();  
$where->lessThanOrEqualTo(      
        'FROM_UNIXTIME(UNIX_TIMESTAMP(epg_start) + epg_duration)', '2013-12-01 01:30:00'  
    )
    ->greaterThanOrEqualTo('epg_start', '2013-11-30 11:00:00')
    ->equalTo('epg_service', 'skytv')
    ->equalTo('epg_channel', '4music'); 
$this->delete($where);

...我收到以下错误

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`+` `epg_duration``)` <= '2013-12-01 00:28:10' AND `epg_start` >= '2013-12-01 00'

我尝试过使用Zend Expression类,但是我收到了错误

preg_split() expects parameter 2 to be string, object given

任何人都可以给我一些建议/示例,说明如何使用“Where”类

构建此查询

提前致谢

1 个答案:

答案 0 :(得分:6)

经过一个小时或更长时间浏览Zend库后,我终于弄明白了

    $where = new Where();  
    $where->greaterThanOrEqualTo('epg_start', '2013-11-30 11:00:00')
        ->equalTo('epg_service', 'skytv')
        ->equalTo('epg_channel', '4music'); 
    $where->addPredicate(new \Zend\Db\Sql\Predicate\Expression('FROM_UNIXTIME(UNIX_TIMESTAMP(epg_start) + epg_duration) <= ?'), '2013-12-01 01:30:00');
    $this->delete($where); 

我初步尝试\ Zend \ Db \ Sql \ Expression但它应该是\ Zend \ Db \ Sql \ Predicate \ Expression