以下代码是否可以安全删除数据?
public function remove($sc1_id = false)
{
if(!$sc1_id) redirect('backend/sections/index');
$sub_section_ids = $this->flatten($this->db->select('sc2_id')->from('sub_sections')->join('sections', 'sc2_sc1_id = sc1_id', 'INNER')->where('sc1_id', $sc1_id)->get()->result_array());
if($sub_section_ids)
{
$this->db->where_in('s2l_sc2_id', $sub_section_ids);
$this->db->delete('sub_section_prod_link');
}
$this->db->where('sc1_id', $sc1_id);
$this->db->delete('sections');
$this->db->where('sc2_sc1_id', $sc1_id);
$this->db->delete('sub_sections');
redirect('backend/be_sections/index');
}
我想确保where语句被填满。在测试中,数据已经被删除,所以也许$sc1_id
是真实的,但仍然没有在where语句中做出条件,或者我也需要flush_cache()
或reset_query()
?
答案 0 :(得分:1)
我要补充一点,它是一个交易声明。您在删除过程中使用了多个表,如果其中一个表失败,则无法返回。所以:
$this->db->trans_begin();
//your statements here
//if something goes wrong, just undo
if($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
//if everything is ok, proceed
else
{
$this->db->trans_commit();
redirect('to_somewhere');
}