如何在php mysql中删除带有子类别的类别时,如何更新帖子类别关系?

时间:2014-08-08 11:53:35

标签: php mysql

在我的博客系统中,我有多对多的关系。我有三张桌子即。 tb_categories,tb_posts和tb_post_cat_relationships。这些在下面给出 -

tb_categories -

enter image description here

tb_posts -

enter image description here

tb_post_cat_relationships -

enter image description here

您可以理解这三个表之间的关系。现在当一个类别被删除然后发布在它下面时会发生什么情况将被分配给Uncategorized(它的id是-1,它不存在于tb_categories表中)。如果父类别被删除,那么其所有子类别也将被删除,但所有属于父类别和子类别的帖子将与未分类相关联,即-1 id。

这里的问题始于我。如果不同的帖子与这些父类别和子类别相关联,则其确定 - 我将所有帖子更新为-1,对应于这些类别ID。但是如果一个帖子与几个类别及其子类别关联,则会创建大量重复行,因为id为1的单个公共帖子可能与类别1及其子类别2,3,4等相关联。所以会有是使用重复数据创建的4行,如

enter image description here

因为我删除了父类别Web开发,所以它的子类别PHP也被删除,因此属于这两个类别的帖子现在被分配到-1未分类类别。

这就是问题所在。我想要一个帖子只有一个唯一的行。从现在开始分享多个类别并分配给Uncategorized -1所以应该只有一行像这样 -

enter image description here

我该如何解决这个问题?我的PHP代码如下 -

当我删除该类别时,会调用此代码 -

    $where = $_GET['cat_id'];
    $status = $db_obj->update_post_cat_relationship($where)->delete_category($where);

这是更新表并删除类别的代码 -

// update post category relationship
public function update_post_cat_relationship($where)
{
    if(is_array($where) && !empty($where))
    {
        $cat_id = implode(", ", $where);

        // first update all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->update_post_cat_relationship($row['category_id']);
            }
        }

        // then update the parent category
        $query = "UPDATE `tb_post_cat_relationships` SET `cat_id` = -1 WHERE `cat_id` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        //$affected_rows = mysqli_affected_rows($this->con);
        if($rs)
        {
            return $this;
        }
        else
        {
            return false;
        }
    }
    elseif(!is_array($where) && !empty($where))
    {
        // first update all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` = " . $where;
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->update_post_cat_relationship($row['category_id']);
            }
        }

        // then update the parent category
        $query = "UPDATE `tb_post_cat_relationships` SET `cat_id` = -1 WHERE `cat_id` = " . $where;
        $rs = mysqli_query($this->con, $query);
        //$affected_rows = mysqli_affected_rows($this->con);
        if($rs)
        {
            return $this;
        }
        else
        {
            return false;
        }
    }
}

// delete category and subcategories    
public function delete_category($where)
{
    if(is_array($where) && !empty($where))
    {
        $cat_id = implode(", ", $where);

        // first delete all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` IN (" . $cat_id . ")";
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->delete_category($row['category_id']);
            }
        }

        // then delete the parent category
        $query = "DELETE FROM `tb_categories` WHERE `category_id` IN (" . $cat_id . ")";
        mysqli_query($this->con, $query);

        $affected_rows = mysqli_affected_rows($this->con);
        if($affected_rows)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    elseif(!is_array($where) && !empty($where))
    {
        // first delete all the sub-categories
        $query = "SELECT `category_id` FROM `tb_categories` WHERE `category_parent` = " . $where;
        $rs = mysqli_query($this->con, $query);
        if(mysqli_num_rows($rs) > 0)
        {
            while($row = mysqli_fetch_array($rs))
            {   
                $this->delete_category($row['category_id']);
            }
        }

        // then delete the parent category
        $query = "DELETE FROM `tb_categories` WHERE `category_id` = " . $where;
        mysqli_query($this->con, $query);

        $affected_rows = mysqli_affected_rows($this->con);
        if($affected_rows)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用foreign keys级联删除。删除它将删除关系或设置NULL值。