在application新建一個文件夾migrations,存放建表類。php
建表類使用用戶手冊中的代碼做爲模板(user_guide/libraries/migration.html)html
class Migration_Add_blog extends CI_Migration { public function up() { $this->dbforge->add_field(array( 'blog_id' => array( 'type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE ), 'blog_title' => array( 'type' => 'VARCHAR', 'constraint' => '100', ), 'blog_description' => array( 'type' => 'TEXT', 'null' => TRUE, ), )); $this->dbforge->create_table('blog'); } public function down() { $this->dbforge->drop_table('blog'); } }
(1) 新建migrations/001_create_users.php瀏覽器
(2) 新建migrations/002_create_pages.phpapp
新建一控制器controllers/admin/migration.phpide
其主要的函數爲:函數
public function index() { $this->load->library('migration'); if (! $this->migration->current()) { show_error($this->migration->error_string()); } else { echo 'Migration worked!'; } }
修改config/migration.php中:ui
$config['migration_enabled'] =FALSE;this
變動爲spa
$config['migration_enabled'] =TRUE;code
每次咱們增長數據表時要增長這個版本值!
$config['migration_version'] = 0;
變動爲
$config['migration_version'] = 1;
------------------------------------------------------轉載請註明:xiaobin_hlj80----------------------------------
最後,在瀏覽器地址欄輸入:http://127.0.0.1/testCI3/public_html/admin/migration
此時,會多出3個數據表:migrations, pages, users
users:
pages:
migrations(使用migration生成數據表必生成它)