如何删除codeigniter中的子类别

问题描述 投票:0回答:6

删除主分类后,如何删除所有子分类及其内部分类?

桌子

这里如果我删除类别“计算机”意味着如何也删除其子类别(鼠标,键盘,虚拟,触摸)?

php mysql codeigniter cascading-deletes
6个回答
2
投票

您可以选择以下选项之一来实现此目的:

1- 您设置数据库以强制引用完整性,然后就像告诉 MySQL 一样简单

ON DELETE CASCADE;

注意已测试,但我想它应该可以工作。

ALTER TABLE categories
ADD FOREIGN KEY (parent_id)
REFERENCES Categories(id)
ON DELETE CASCADE;

2-第二个选项是递归调用删除函数来完成此操作。 示例:

public function delete_category_by_id ($category_id) {
    // delete this category.
    $this->db->delete('categories', ['id' => $category_id]);
    // fetch child categories & call the same method again.
    $q = $this->db->where('parent_id', $category_id)->get('categories');
    foreach( $q->result() as $Child ) {
        $this->delete_category_by_id($Child->id);
    }
}

这将是非常具有破坏性的,并且可能需要很长时间,具体取决于你有多少级别


1
投票
function delete_parent_and_child_subchild($category_id,$all_cate=array())
{
    if(!is_array($category_id))
    {           
        $this->db->where('parent_id', $category_id);
        $all_cate[]=$category_id;
    }
    else
    {           
        $this->db->where_in('parent_id', $category_id);
    }        
    $get_categories= $this->db->get('newcategory'); 
    if($get_categories->num_rows()>0)
    {
        $categories_vales=$get_categories->result(); 
        $new_subcat = array();
        foreach($categories_vales as $cate_val)
        {
            $category_id=$cate_val->category_id;
            array_push($new_subcat,$category_id);
        }
        $all_cate = array_merge($all_cate,$new_subcat);         
        if(count($new_subcat)>0)
        {
            $this->delete_parent_and_child_subchild($new_subcat,$all_cate);
        }   
    }   
    $this->db->where_in('category_id', $all_cate)->delete('newcategory');
    return true;
}

0
投票

如果您根据ID删除类别,请将这些ID存储在一个变量中,然后根据parent_id=ID再次进行另一个删除查询。

根据类别 ID 删除行:

$SQL = "delete from category where id=9";

根据类别ID删除子类别:

$SQL = "delete from category where parent_id=9";

或者您可以在一个查询中执行这两个条件。

$SQL = "delete from category where id=9 or parent_id=9";

0
投票

是的,您可以通过单个查询来完成

$query = "delete from category where id=1 or parent_id=1";

0
投票

父表

id  value
1 computer
2 electronics

子表

child_tbl_pk_id parent_table_id   value
1                     1           mouse
2                     1           touch
3                     2           fan

询问:

q1 = "DELETE FROM parent_table WHERE id = 1"
q2 = "DELETE FROM child_table WHERE parent_table_id = 1"

0
投票

通过传递parent_id()来运行此代码并使用您的表更改表名称

function delete_parent_and_child_subchild($category_id,$all_cate=array())
{
    if(!is_array($category_id))
    {           
        $this->db->where('parent_id', $category_id);
        $all_cate[]=$category_id;
    }
    else
    {           
        $this->db->where_in('parent_id', $category_id);
    }        
    $get_categories= $this->db->get('newcategory'); 
    if($get_categories->num_rows()>0)
    {
        $categories_vales=$get_categories->result(); 
        $new_subcat = array();
        foreach($categories_vales as $cate_val)
        {
            $category_id=$cate_val->category_id;
            array_push($new_subcat,$category_id);
        }
        $all_cate = array_merge($all_cate,$new_subcat);         
        if(count($new_subcat)>0)
        {
            $this->delete_parent_and_child_subchild($new_subcat,$all_cate);
        }   
    }   
    $this->db->where_in('category_id', $all_cate)->delete('newcategory');
    return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.