【问题标题】:Codeigniter 3 migrations: add the Uncategorized posts category at the first migrations runCodeigniter 3 迁移:在第一次迁移运行时添加未分类的帖子类别
【发布时间】:2018-10-09 02:49:31
【问题描述】:

我正在使用 Codeigniter 3.1.8Bootstrap 4 开发一个基本的博客应用程序。

我使用迁移文件(001_create_authors.php005_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');
  }

}

为了在类别表中插入默认的“未分类”类别,我必须在上面的代码中添加什么,第一次迁移运行

【问题讨论】:

  • default 约束添加到列中。
  • $this->dbforge->create_table('categories'); //your last code $data = array( 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_batch('categories', $data);

标签: php codeigniter codeigniter-3


【解决方案1】:
$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多