php - Codeigniter 3 迁移:在第一次迁移运行时添加未分类的帖子类别
问题描述
我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序。
我使用迁移文件(001_create_authors.php
最多005_create_comments.php
)来自动创建必要的数据库表。
除了创建categories
表格之外,我还需要在其中插入默认的“未分类”类别,因为帖子必须属于一个类别。
迁移的当前代码:
class Migration_Create_Categories extends CI_Migration
{
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,
),
'name'=>array(
'type'=>'VARCHAR',
'constraint' => 255,
),
'created_at'=>array(
'type'=>'TIMESTAMP',
)
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('categories');
}
public function down()
{
$this->dbforge->drop_table('categories');
}
}
为了在第一次迁移运行时在类别表中插入默认的“未分类”类别,我必须在上面的代码中添加什么?
解决方案
$this->dbforge->create_table('categories');
//after this line
$data = array(
//'id' => leave this as it will auto created
'author_id' => 1,
'name' => 'Uncategorized'
//'created_at' => leave it as this will auto created
);
$this->db->insert('categories', $data);
推荐阅读
- javascript - p5.js 使用 setInterval、array.push()、for 循环创建动画时出现问题
- php - 如何让 Wordpress 编辑器访问 Google 插件的 Site Kit?
- json - Bash 脚本 / JSON - 从 {"Key":"Duration","Value":"N"} 中提取数值
- python - 在 matplotlib 中更改字体无法正常工作
- orm - 在没有 UI 事件的情况下更改模型时的 Odoo v12
- java - 在外部 Spring Boot Java 项目中访问属性 pf application.properties
- regex - Notepad ++如何删除特定字符和符号组合之前的所有字符
- javascript - AngularJS 元素指令不在 HTML 上呈现
- mysql - 配置文件中的 ProxySQL SSL 后端配置
- javascript - Vue JS removeEventListener 不起作用。为什么?