Zend_Db - 构建删除查询
我正在尝试在 Zend Framework 中重新创建以下内容,但不知道该怎么做:
DELETE FROM mytablename WHERE date( `time_cre` ) < curdate( ) - INTERVAL 4 DAY
我在想:
$table = $this->getTable();
$db = Zend_Registry::get('dbAdapter');
$db->delete($table, array(
'date(`time_cre`) < curdate() - interval 4'
));
这看起来正确吗?
处理这样的事情最好的方法是什么?
编辑: 嗯!抱歉,我正在从用于测试的 SELECT 中进行调整,并且在粘贴它时没有正确更改语法。我已经编辑了示例来修复它。
I'm trying to recreate the following in Zend Framework and am not sure how to do it:
DELETE FROM mytablename WHERE date( `time_cre` ) < curdate( ) - INTERVAL 4 DAY
I was thinking something like:
$table = $this->getTable();
$db = Zend_Registry::get('dbAdapter');
$db->delete($table, array(
'date(`time_cre`) < curdate() - interval 4'
));
Does that seem correct?
What is the best way to handle something like this?
EDIT: Ack! Sorry, I was adapting this from a SELECT I was using to test and didn't change the syntax properly when I pasted it in. I've edited the example to fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
弄清楚了...
$table
获取我想要编辑的表的引用...$db
获取数据库适配器的实例,以便我可以使用quoteInto()
...$where
构建接受$days
的查询的主要部分,以使事情更加灵活。创建一个操作来调用此方法...类似于:
现在点击:
http://myhost/thiscontroller/pruneold/
将删除条目。当然,任何点击该网址的人都会删除这些条目,但我已经采取了示例中未包含的步骤来处理此问题。希望这对某人有帮助。Figured it out...
$table
gets an reference of the table I want to edit...$db
grabs an instance of the database adapter so I can usequoteInto()
...$where
builds the main part of the query accepting$days
to make things a bit more flexible.Create an action to call this method... something like:
And now hitting:
http://myhost/thiscontroller/pruneold/
will delete the entries. Of course, anyone hitting that url will delete the entries but I've taken steps not included in my example to deal with this. Hope this helps someone.我通常这样做是为了删除一行
I usually do this to delete a row
尝试:
$db->删除($表, 数组(
'日期(time_cre)< ? => new Zend_Db_Expr('curdate() - 间隔 4')
));
try:
$db->delete($table, array(
'date(time_cre) < ?' => new Zend_Db_Expr('curdate() - interval 4')
));