基本上,我想做的是用户将选中复选框并单击 deleteAll 按钮一次删除多条记录。为此,我触发 ajax 并获取所有选定的 ID,然后将选定的 ID 发送到 Controller 的方法。
我正在使用 delete 方法从数据库中删除多条记录,但它不起作用。
而且我不想使用循环删除多条记录。
CakePHP 有没有其他的一次性删除多条记录的方法?
我试过下面的代码:
脚本.js :
$('#del_all').click(function(){
var selected=[];
$('.check:checked').each(function(){
selected.push($(this).attr('id'));
});
$.ajax({
type: "post",
url: "Users/deleteall", // URL to request
data: {"id":selected}, // Form variables
success: function(response){
alert(response);
}
});
});
Controller 方法:
public function deleteall(){
$data=$this->request->data['id'];
$user_ids=implode("','",$data);
$user_ids="'".$user_ids."'";
$this->User->delete($user_ids,$cascade=false);
}
最佳答案
尝试使用 deleteall
尝试使用此
public function deleteall()
{
$user=array(1,2,3); // replace with real values
$condition = array('User.id in' => $user);
$this->User->deleteAll($condition,false);
}
关于php - 如何在 CakePHP 2.6 中使用 IN(of MySQL) 一起删除多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31740195/