简体   繁体   中英

CodeIgniter - Ensuring activerecord where statement has a criteria

Is the following code safe for deleting data?

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');
}

I want to make sure that the where statements are being fullfilled. In testing, data has been wiped, so perhaps $sc1_id was truey, yet still not making a condition on the where statement, or perhaps I need to flush_cache() or reset_query() as well?

One thing I would add, it's a transaction statement. You're using more than one table for the delete process, if one of then fails you can't go back. So:

$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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM