Codeigniter 3 博客应用程序:每当删除一个类别时,将该类别中所有帖子的 cat_id 设置为 1

Codeigniter 3 blogging application: whenever a category is deleted, set cat_id of all posts in that category to 1

我正在使用 Codeigniter 3.1.8Bootstrap 4.[=19= 开发一个基本的博客应用程序]

有一个posts和一个类别table。目前,应用程序将 类别 ID 插入 posts table,每当 post被创建(你不能在为它选择类别之前添加post),但我已经设置了两个table之间没有外键关系(为了便于开发)和无级联

现在我想在两个 table 之间设置一个特殊的级联关系:每当删除一个类别时,该类别中的所有 post 都应该在 cat_id列。

我已经为该应用程序创建了一个安装过程:在创建数据库并将其凭据提供给 application/config/database.php 文件后,您可以 运行 Install 控制器将创建所有必要的 tables:

class Install extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
    }

    public function index(){
        // Create all the database tables if there are none
        // by redirecting to the Migrations controller
        $tables = $this->db->list_tables();
        if (count($tables) == 0) {
            redirect('migrate');
        } else {
            redirect('/');
        }
    }
}

我用来创建 posts table 的迁移是:

public function up(){
  $this->dbforge->add_field(array(
  'id'=>array(
    'type'=>'INT',
    'constraint' => 11,
    'unsigned' => TRUE,
    'auto_increment' => TRUE
  ),

  'author_id'=>array(
    'type'=>'INT',
    'constraint' => 11,
    'unsigned' => TRUE,
  ),

  'cat_id'=>array(
    'type'=>'INT',
    'constraint' => 11,
    'unsigned' => TRUE,
  ),

  'title'=>array(
    'type'=>'VARCHAR',
    'constraint' => 255,
  ),

  'slug'=>array(
    'type'=>'VARCHAR',
    'constraint' => 128,
    'unique' => TRUE,
  ),

  'description'=>array(
    'type'=>'VARCHAR',
    'constraint' => 255,
  ),

  'content'=>array(
    'type'=>'TEXT',
  ),

  'post_image'=>array(
    'type'=>'VARCHAR',
    'constraint' => 255,
  ),

 'created_at'=>array(
    'type'=>'TIMESTAMP',
  ),

 'updated_at'=>array(
    'type'=>'TIMESTAMP',
  ),

));

$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('posts');
$this->db->query('ALTER TABLE `posts` ADD FOREIGN KEY(`cat_id`) REFERENCES 'categories'(`id`) ON DELETE SET cat_id to 1;');

}

我应该用什么替换最后一行(它具有说明性目的):

$this->db->query('ALTER TABLE `posts` ADD FOREIGN KEY(`cat_id`) REFERENCES 'categories'(`id`) ON DELETE SET cat_id to 1;');

为了得到想要的结果?

更新: 我的数据库 使用 InnoDB.

您需要使用

`ALTER TABLE `posts` ADD FOREIGN KEY(`cat_id`) REFERENCES 'categories'(`id`) ON DELETE SET DEFAULT 1`

但是,请注意这 不适用于 InnoDB https://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

这是一个使用 php

的例子
function_delete_category($id) {

     $this->db->trans_start();

     $this->db->where('cat_id', $id);
     $ct = $this->db->count_all_results('posts');

     if ($ct > 0) {

         $this->db->set('cat_id', 1);
         $this->db->where('cat_id', $id);
         $this->db->update('posts');

     }

     $this->db->delete('categories', array('id' => $id));

     $this->db->trans_complete();

     return $this->db->trans_stats();

}
  • 老实说,如果类别被删除,您应该将 cat_id 列作为外键并进行软删除。

  • 但是,如果有必要,您可以为此创建一个 trigger,以便 MySQL 负责删除和更新,如下所示:

触发器SQL:

delimiter //

create trigger update_post_categories_with_1
before delete on categories for each row

begin
    update posts set cat_id = 1 where cat_id = OLD.id;  
end//

delimiter ;