我有一个商店桌子和一个项目桌子的家伙。所以我想从商店表中删除商店。如果商店中没有商品,则应将其删除;如果商店中没有商品,则不能将其删除。此处编写的代码不会删除商店,无论是否有物品。这是我的控制器:
public function deleteShop()
{
if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
{
redirect('auth/login', 'refresh');
}
else
{
$id = 0;
$shop_id = $_GET['shop_id'];
$results = $this->shop_model->getShopIdFromItem($shop_id);
if($results) {
foreach ($results as $key) {
$id = $key->shop_id;
}
if($id == $shop_id) {
$this->session->set_flashdata('message', 'There is an item with this Shop. You cannot delete this!');
redirect(base_url() . 'admin/shop','refresh');
}
} else {
$result = true;
if($result) {
redirect(base_url() . 'admin/shop', 'refresh');
} else {
echo "Something went wrong!";
}
}
}
这是我的模特
function getShopIdFromItem($shop_id) {
$this->db->select('category_id');
$this->db->from('item');
$this->db->where('shop_id', $shop_id);
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
function delete($shop_id)
{
$this->db->where('shop_id', $shop_id);
$this->db->delete('shop');
return $this->db->affected_rows();
}
答案 0 :(得分:0)
function delete($shop_id)
{
$this->db->from('item');
$this->db->where('shop_id', $shop_id);
$query = $this->db->get();
if($query->num_rows() == 0) {
$this->db->where('shop_id', $shop_id);
$this->db->delete('shop');
return $this->db->affected_rows();
} else {
return false;
}
更改您的删除方法...仅在没有可用项目的情况下才会删除..