0

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.

7
  • 1
    @rg88 it's not clear what you need to delete. The syntax of delete is DELETE FROM [table_name] Commented Mar 3, 2011 at 19:03
  • what dbms are you using (I would assume mysql?) Your syntax is non-standard and rather odd. If you only want to remove data from one column you should use UPDATE and set the column to NULL,'',0, etc. DELETE is specifically for removing entire rows, unless your dbms supports something i'm unfamiliar with. Commented Mar 3, 2011 at 19:05
  • From where you get that syntax: DELETE [something] FROM table [where] ? Neither MySQL, PostgreSQL or Oracle uses that. After DELETE you don't need anything, because you're deleting whole row, not column value. Commented Mar 3, 2011 at 19:06
  • DELETE removes the full row , n Commented Mar 3, 2011 at 19:25
  • @criticus @Matt @singles - I failed to edit the example properly. I've updated my example.. sorry about that. Commented Mar 3, 2011 at 19:31

3 Answers 3

2

Figured it out...

public function pruneOld($days) {
    $table = $this->getTable();
    $db = Zend_Registry::get('dbAdapter');
    $where = $db->quoteInto("DATE(`time_cre`) < CURDATE() - INTERVAL ? DAY", $days);

    return $table->delete($where);
}

$table gets an reference of the table I want to edit...

$db grabs an instance of the database adapter so I can use quoteInto()...

$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:

public function pruneoldAction() {

    // Disable the view/layout stuff as I don't need it for this action
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    // Get the data model with a convenience method
    $data = $this->_getDataModel();

    // Prune the old entries, passing in the number of days I want to be older than
    $data->pruneOld(2);
}

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I assume, that $data inherits from Zend_Db_Table_Abstract. So within your pruneOld method you have access to db adapter using $this->getAdapter() - you don't have to take it from registry. Second thing - inside model you have also method delete(), which takes one parameter - where array.
0

I usually do this to delete a row

$table = new yourDB;
$row = $table->fetchRow($table->select()->where(some where params));

$row->delete();

1 Comment

the problem with this form is you're processing two queries, minimum (one for the select, one for the delete) to perform the delete operation. the pdo delete action accepts the same parameters and only performs one query.
0

try: $db->delete($table, array( 'date(time_cre) < ?' => new Zend_Db_Expr('curdate() - interval 4') ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.