如何在 PHPLucidFrame 中删除 db_delete() 范围内的多条记录?

How to delete multiple records in range with db_delete() in PHPLucidFrame?

我在 PHPLucidFrame 中使用 db_delete() 删除了一段时间内的多条记录。有什么办法可以实现吗?

首先,你不应该使用db_delete()来删除多条记录。仅针对单条记录删除,检查外键约束,如果可能的话,决定对记录进行逻辑删除还是物理删除。以下引自its documentation:

db_delete() is a handy method for your SQL DELETE operation. This is only applicable for single record deletion. The syntax is db_delete('table_name', $condition=NULL). LucidFrame encourages MYSQL Foreign Key Constraints to use. If ON 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)

相反,您必须使用 db_delete_multi() 删除多条记录:

db_delete_multi() is useful for batch record deletion for the given condition, but it does not check foreign key constraints.

例如,这里是如何删除在使用 db_delete_multi()db_and() 期间创建的多条记录:

db_delete_multi('post', db_and(
    array('created >=' => '2015-01-01'), 
    array('created <=' => '2015-01-31')
  )
);

更简单的解决方案是在条件数组中使用 between 运算符。在这种情况下,您不需要使用 db_and().

db_delete_multi('post', array('created between' => array('2015-01-01', '2015-01-31')));

您至少需要使用 PHPLucidFrame 版本 1.2