Zend Framework: How to delete a table row where multiple things are true?
Asked Answered
S

3

26

Normally, this would work for me:

$db = Zend_Db_Table::getDefaultAdapter();
$where = $db->quoteInto('id = ?', $id);
$db->delete('tablename', $where);

but I have to match two ids. So I don't really know how to structure it.

WHERE first_id = 'id1' AND second_id = 'id2'

So how do I do this with the Zend Framework?

Sse answered 4/12, 2009 at 6:5 Comment(0)
P
59

To extend on Jason W's answer:

Not exactly sure what the 3rd section is saying

That means you can do this:

$db->delete('tablename', array(
    'first_id = ?' => $first_id,
    'second_id = ?' => $second_id
));

And the adapter will quote everything for you.

I don't feel like the documentation is very clear though.

Plumcot answered 4/12, 2009 at 8:7 Comment(3)
Always check the API - the manual simply can't cover everything in enough detailAnimalism
An API that forces you to reverse engineer it to do simple things is a crappy API.Choosy
Exactly @AlanStorm, to really use Zend Framework, you really have to dive into the source code. Makes for a steep and frustrating learning curve.Trefoil
P
20

From the zend manual on delete():

If you omit the second argument, the result is that all rows in the database table are deleted.

If you provide an array of strings as the second argument, these strings are joined together as terms in an expression separated by AND operators.

If you provide an array of arrays as the second argument, the the values will be automatically quoted into the keys. These will then be joined together as terms, seperated by AND operators.

Not exactly sure what the 3rd section is saying, but the 2nd implies that you can do:

$where = array();
$where[] = $db->quoteInto('first_id = ?', $first_id);
$where[] = $db->quoteInto('second_id = ?', $second_id);
$db->delete('tablename', $where);
Pithecanthropus answered 4/12, 2009 at 6:32 Comment(1)
That's the better way to do it!Neighborhood
S
2

If you're in model which is extending class Zend_Db_Table_Abstract, you need to use different structure:

class Yourmodel extends Zend_Db_Table_Abstract {
    protected $_name = 'tablename';
    protected $_primary = 'primarykey';
    public function remove($first_id, $second_id) {
        $where = array();
        $where[] = $this->getAdapter()->quoteInto('first_id = ?', $first_id);
        $where[] = $this->getAdapter()->quoteInto('second_id = ?', $second_id);
        $this->delete($where);
    }
}
Singhal answered 3/9, 2013 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.