I'm deleting multiple records within a period using db_delete()
in PHPLucidFrame. Is there any way to achieve this?
Firstly, you should not use db_delete()
to delete multiple records. It is just for single record deletion which checks foreign key constraint to determine to perform logical delete or physical delete on the record if possible. The below is quote from its documentation:
db_delete()
is a handy method for your SQL DELETE operation. This is only applicable for single record deletion. The syntax isdb_delete('table_name', $condition=NULL)
. LucidFrame encourages MYSQL Foreign Key Constraints to use. IfON DELETE RESTRICT
is found, it performs soft delete (logical delete) by updating the current date/time into the field “deleted”, otherwise it performs hard delete (physical delete)
Instead, You have to use db_delete_multi() to delete multiple records:
db_delete_multi()
is useful for batch record deletion for the given condition, but it does not check foreign key constraints.
For example, here is how to delete multiple records which were created in a period using db_delete_multi()
and db_and()
:
db_delete_multi('post', db_and(
array('created >=' => '2015-01-01'),
array('created <=' => '2015-01-31')
)
);
For more simple solution is to use between
operator in your condition array. In this case, you don't need to use db_and()
.
db_delete_multi('post', array('created between' => array('2015-01-01', '2015-01-31')));
You would need to use PHPLucidFrame version 1.2 at least.