Zend: How to use multiple conditions at delete time?

September 20, 2011 | In: mysql, php, web development, Zend Framework

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:

<i>DELETE FROM tbl_name WHERE conditionField1 = 'value1' AND conditionField2 = 'value2';</i>

That’t it.

Comment Form

Leave a Reply

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

*

Categories

Latest comments


    Fatal error: Uncaught Error: Call to undefined function split() in /home4/nos/public_html/naveenos.com/wp-content/themes/smoker/functions.php:72 Stack trace: #0 /home4/nos/public_html/naveenos.com/wp-content/themes/smoker/sidebar.php(11): mdv_recent_comments() #1 /home4/nos/public_html/naveenos.com/wp-content/themes/smoker/single.php(47): include('/home4/nos/publ...') #2 /home4/nos/public_html/naveenos.com/wp-includes/template-loader.php(74): include('/home4/nos/publ...') #3 /home4/nos/public_html/naveenos.com/wp-blog-header.php(19): require_once('/home4/nos/publ...') #4 /home4/nos/public_html/naveenos.com/index.php(17): require('/home4/nos/publ...') #5 {main} thrown in /home4/nos/public_html/naveenos.com/wp-content/themes/smoker/functions.php on line 72