Codeigniter3のMigrationでテーブル作成。
8154 ワード
Codeigniter3のマイグレーションでテーブルを作ってみる。
Codeigniter3はComposerでインストール済み
データベース設定
Vagrantで試したのでポート番号追記
config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'test',
'dbdriver' => 'mysqli',
・・・
'port' => 3306
);
マイグレーション設定
今回はファイルに連番をつけてやる設定で。
config/migrate.php
$config['migration_enabled'] = TRUE;// 有効
$config['migration_type'] = 'sequential';// 連番 or タイムスタンプ
$config['migration_version'] = 1; // バージョン
マイグレーションファイル作成
今回はバージョンを1としますのでファイル名に001を振ります。
migrations/001_add_users.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Add_Users extends CI_Migration
{
public function up()
{
$this->dbforge->add_field([
'id' => [
'type' => 'INT',
'unsigned' => TRUE,
'auto_increment' => TRUE
],
'email' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'password' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
]);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('users');
}
public function down()
{
$this->dbforge->drop_table('users');
}
}
マイグレーションコントローラー作成
application/controllers/Migrate.php
<?php
class Migrate extends CI_Controller
{
public function index()
{
$this->load->library('migration');
if ($this->migration->current() === FALSE) {
show_error($this->migration->error_string());
}
}
}
マイグレーション実行
vagrant sshでサーバーに入ります。
index.phpを設置したディレクトリに移動します。
cd public
php index.php migrate
usersテーブルが作成されました。
Author And Source
この問題について(Codeigniter3のMigrationでテーブル作成。), 我々は、より多くの情報をここで見つけました https://qiita.com/niiyz/items/743a20b221b7418d04e8著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .