[ codeigniter ] Migration 사용방법 > Codeigniter

본문 바로가기

사이트 내 전체검색

Codeigniter

팁자료 [ codeigniter ] Migration 사용방법

작성일 22-01-11 15:31

페이지 정보

작성자 웹지기 조회 2,869회 댓글 0건

본문

ciboard 에서 migrations 클래스 사용법

 

1. 마이그레이션 활성화 비활성화

 - application/config/migration.php 14줄

   $config['migration_enabled'] = FALSE;  부분을  => TRUE로 변경

 - 32줄 타입선택 ==> timestamp

 - $config['migration_version'] = 0;   ===> 버젼설정

 - 폴더설정 ==> $config['migration_path'] = APPPATH.'migrations/'; 

 

2. 마이그레이션 만들기 

 - application/migrations/20220111090000_blog.php  

   (블로그에 대한 DB를 만들때 예제 - timestamp로 설정된 값 _ db명.php )

 - 코드 내용 대략

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Blog extends CI_Migration {

    public function up() {

            $this->dbforge->add_field(array(

            'id' => array(

                    'type' => 'INT',

                    'constraint' => 5,

                    'unsigned' => TRUE,

                    'auto_increment' => TRUE

            ),

            'name' => array(

                    'type' => 'VARCHAR',

                    'constraint' => '100'

            ),

            'description' => array(

                    'type' => 'TEXT',

                    'null' => TRUE

            ),

        ));

        $this->dbforge->add_key('id', TRUE);

        $this->dbforge->create_table('blog');

    }


    public function down()

    {

        $this->dbforge->drop_table('blog');

    }

}

 

3. 실행 클래스 만들기

 - application/controllers/Migrate.php

 <?php

defined('BASEPATH') OR exit('No direct script access allowed');


/**

 * 마이페이지와 관련된 controller 입니다.

 */

 class Migrate extends CB_Controller

{

    /**

     * 모델을 로딩합니다

     */

    protected $models = array();


    /**

     * 헬퍼를 로딩합니다

     */

    protected $helpers = array('form', 'array');


    function __construct()

    {

        parent::__construct();


        /**

         * 라이브러리를 로딩합니다

         */

        $this->load->library(array('pagination', 'querystring'));


    }


    public function index()

    {

        // 이벤트 라이브러리를 로딩합니다

        $eventname = 'event_mypage_index';

        $this->load->event($eventname);


        $view = array();

        $view['view'] = array();


        $this->load->library('migration');

        if ($this->migration->current() === FALSE)

        {

            echo $this->migration->error_string();

            //show_error($this->migration->error_string());

        }

        else

        {

            echo 'Table Migrated Successfully.';

        }

    }

 

소스코드 실행( ciboard 기준 )  ::: http://localhost/migrate
이런식으로 실행을 하게 되면 DB table이 없다면 새로 생성이 되고 있다면 버젼을 바꾸게 되겠지요??


추천0

비추천 0

댓글목록

등록된 댓글이 없습니다.

전체 20건 1 페이지

이미지 목록

게시물 검색
Copyright © 즐거운 코딩 생활 ( funyphp ). All rights reserved.
PC 버전으로 보기