一点代码片段:
$this->db->where('id', $outfit_id);
$this->db->update('outfit_main',$data);
//Delete productsettings for specific outfit (They are added below with new values)
$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_products');
//Delete outfit_images for specific outfit as well. They are also added below
$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_images');
我想做的是:
但实际上当我添加最后两行时:
$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_images');
* 它也会删除outfit_main中的所有行。为什么?*
答案 0 :(得分:6)
尝试将where子句与删除活动记录结合使用:
$this->db->delete('outfit_main', array('id' => $outfit_id));
$this->db->delete('outfit_products', array('outfit_id' => $outfit_id));
$this->db->delete('outfit_images', array('outfit_id' => $outfit_id));
答案 1 :(得分:1)
似乎Ci
正在对old/cached
子句使用where
查询,因此如果flush
使用<{p>}清除缓存
$this->db->flush_cache();
然后使用
$this->db->where('outfit_id', $outfit_id);
$this->db->delete('outfit_images');
然后,也许它会以正确的方式运作。 检查caching示例,它会更清楚,我也会对此感到困惑,所以等待回复。
答案 2 :(得分:1)
$this->db->join("table2", "table1.thing_id = table2.id")
->where("table2.otherthing_id", $id)
->delete("table1");
您可以使用以下代码从不同的表中删除数据。它会对你有所帮助。
$sql = "DELETE t1 FROM table1 t1
JOIN table2 t2 ON t1.thing_id = t2.id
WHERE t2.otherthing_id = ?";
$this->db->query($sql, array($id));
答案 3 :(得分:0)
如果要从多个表中删除数据,可以将一组表名传递给delete()。更容易的方式
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);