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.
Figured it out…
$tablegets an reference of the table I want to edit…$dbgrabs an instance of the database adapter so I can usequoteInto()…$wherebuilds the main part of the query accepting$daysto 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.