Zend: How to use multiple conditions at delete time?

Zend provides a facility to delete a row from the database table using the delete() method. This method takes one argument, which is an SQL expression that is used in a WHERE clause, as criteria for the rows to delete.

For example :

$modelObj = new ModelName();
$where = $modelObj >getAdapter()->quoteInto('id = ?', '1235');
$modelObj >delete($where);

The second argument can be an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.
Since the table delete() method proxies to the database adapter delete() method, the second argument can be an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.

Sometimes, we need to use multiple conditions to delete a row from database.

We can do this by:

$condition = array(
    'conditionField1 = ?' => 'value1',
    'conditionField2 = ?' => 'value2'
);

We can create an array of conditions and pass to zend db delete function.

$db = Zend_Registry::get("db");
$db->delete('TABLE_NAME', $condition);

It will create query like below to delete records from table:

DELETE FROM tbl_name WHERE conditionField1 = 'value1' AND conditionField2 = 'value2';

That’t it.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*