| 제목 | CI 다중 DB 사용 관련 질문드립니다. | ||
|---|---|---|---|
| 카테고리 | CI 2, 3 | ||
| 글쓴이 | 구리부기 | 작성시각 | 2018/10/16 21:22:50 |
|
|
|||
|
안녕하세요. 구글링과 노가다로 이것저것 해봐서는 도저히 답이 나오지 않아 선배님들의 도움을 요청드립니다.ㅜㅜ CI database.php 에 default 외에 추가 DB를 셋팅한 상태에서 model 단에서 다른 DB 정보를 가져다가 테이블 생성 기능의 소스를 실행하려고 했습니다. 아래는 database.php 의 소스이구요..
$active_group = 'default';
$query_builder = TRUE;
$dbconfig = array(
// default
'default' => array(
'hostname' => '127.0.0.1',
'username' => 'user_01',
'password' => '1234',
'database' => 'db11',
),
'sale_log' => array(
'hostname' => '127.0.0.1',
'username' => 'user_01',
'password' => '1234',
'database' => 'db22',
),
);
foreach($dbconfig as $k => $v) {
$db[$k]['dsn'] = '';
$db[$k]['hostname'] = $v['hostname'];
$db[$k]['username'] = $v['username'];
$db[$k]['password'] = $v['password'];
$db[$k]['database'] = $v['database'];
$db[$k]['dbdriver'] = 'mysqli';
$db[$k]['dbprefix'] = '';
$db[$k]['pconnect'] = FALSE;
$db[$k]['db_debug'] = (ENVIRONMENT !== 'production');
$db[$k]['cache_on'] = FALSE;
$db[$k]['cachedir'] = '';
$db[$k]['char_set'] = 'utf8';
$db[$k]['dbcollat'] = 'utf8_general_ci';
$db[$k]['swap_pre'] = '';
$db[$k]['encrypt'] = FALSE;
$db[$k]['compress'] = FALSE;
$db[$k]['stricton'] = FALSE;
$db[$k]['failover'] = array();
$db[$k]['save_queries'] = TRUE;
}
이렇게 셋팅해놓고 모델 파일의 __construct 에서
$this->db2 = $this->load->database('sale_log'); <- 요렇게도 해보고
메소드에서
$db2 = $this->load->database('sale_log'); <- 요렇게도 불러보고 해봤는데
계속 테이블은 default 로 설정한 DB에 생성이 됩니다.ㅜㅜ 계속 구글링해보면서 이것저것 해보다가 CI 메뉴얼에 있는 메소드 자체에다가도 $config['hostname'] = '127.0.0.1'; $config['username'] = 'user_01'; $config['password'] = '1234'; $config['database'] = 'db22'; $config['dbdriver'] = 'mysqli'; $config['dbprefix'] = ''; $config['pconnect'] = FALSE; $config['db_debug'] = (ENVIRONMENT !== 'production'); $config['cache_on'] = FALSE; $config['cachedir'] = ''; $config['char_set'] = 'utf8'; $config['dbcollat'] = 'utf8_general_ci'; $config['swap_pre'] = ''; $config['encrypt'] = FALSE; $config['compress'] = FALSE; $config['stricton'] = FALSE; $config['failover'] = array(); $config['save_queries'] = TRUE; $this->load->database($config); 이렇게 해서도 해봤는데 여전히 default DB에 생성이 되네요.ㅜㅜ 아래는 모델의 소스 입니다.
class Cron2biz extends CI_Model {
public function __construct() { // {{{
parent::__construct();
} // }}}
// 테이블 생성
public function create_calculate_fee_log() {
$config['hostname'] = '127.0.0.1';
$config['username'] = 'user_01';
$config['password'] = '1234';
$config['database'] = 'db22';
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = (ENVIRONMENT !== 'production');
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';
$config['swap_pre'] = '';
$config['encrypt'] = FALSE;
$config['compress'] = FALSE;
$config['stricton'] = FALSE;
$config['failover'] = array();
$config['save_queries'] = TRUE;
fn_log(222);
$this->load->database($config);
$this->load->dbforge();
try{
fn_log(333);
$this->db->trans_begin();
// 만들 테이블의 필드값 정의
$fields = array(
'cfl_srl' => array(
'type' => 'BIGINT',
'constraint' => 20,
'unsigned' => TRUE,
'auto_increment' => TRUE,
),
'user_srl' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'coin_code' => array(
'type' =>'VARCHAR',
'constraint' => '50',
),
'base_code' => array(
'type' =>'VARCHAR',
'constraint' => '50',
),
'earn_fee' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'od_srl' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'trade_code' => array(
'type' =>'VARCHAR',
'constraint' => '50',
),
'od_qty' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'od_price' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'od_total_price' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'od_fee' => array(
'type' => 'BIGINT',
'constraint' => 20,
),
'fee_type' => array(
'type' => 'TINYINT',
'constraint' => 4,
),
'od_tool_code' => array(
'type' =>'VARCHAR',
'constraint' => '50',
),
'trade_date' => array(
'type' => 'datetime',
'default' => '0000-00-00 00:00:00',
),
);
// 정의한 필드값 적용
$this->dbforge->add_field($fields);
// 인덱스 정의
$this->dbforge->add_key('cfl_srl');
// 테이블명 log_ + 당일 날짜
$tb_name = date('Y-m-d');
$tb_name = 'log_'.$tb_name;
fn_log($tb_name, 'tb_name');
$return = $this->dbforge->create_table($tb_name, TRUE);
// 생성 결과가 없으면 에러 반환
if (empty($return)) except('log_error_01');
fn_log(444);
// 커밋
$this->db->trans_commit();
fn_log(555);
$return = array();
$return['msg'] = 'success';
} catch (Exception $e) {
fn_log(666);
if(!empty($e->getCode())) $this->db->trans_rollback();
$return['msg'] = 'error';
$return['data'] = $e->getMessage();
}
return $return;
}
|
|||
| 다음글 | CI에서 서브도메인에 대해서 로그인 세션 분리 (4) | ||
| 이전글 | PHPExcel 사용을 하려고 합니다. (7) | ||
|
변종원(웅파)
/
2018/10/17 10:27:49 /
추천
0
|
|
한대승(불의회상)
/
2018/10/17 10:54:51 /
추천
0
메뉴얼 자세히 보셔도 잘 이해되지 않으실까봐 힌트 두번째 옵션이 필요합니다. |
|
구리부기
/
2018/10/17 10:56:07 /
추천
0
변종원(웅파)) 아.. 정말 부끄럽습니다. 그렇게 메뉴얼을 봤다고 생각했는데, 왜 $this->db->db_select($database2_name); <- 이걸 못봤을까요. 잘 해결되서 기쁘면서도 부끄럽네요. 더 공부하도록 하겠습니다, 도움주신 웅파님 감사합니다. |
|
구리부기
/
2018/10/17 10:58:00 /
추천
0
한대승(불의회상) 네...^^ 왜 두번째 옵션을 못봤는지 모르겠어요.. 이 한줄 때문에 거의 하루를...ㅜㅜ 웅파님 분의회상님 두분 모두 감사드려요~ |
|
변종원(웅파)
/
2018/10/17 11:06:11 /
추천
0
__construct 에서 아래와 같이 선언하고
$this->db2 = $this->load->database('sale_log', true);
$this->db2->query() 형태로도 사용합니다. ^^ |
ci는 매뉴얼이 잘되어 있는 편입니다. 상당히 많은 질문들이 매뉴얼에서 해결됩니다. ^^
매뉴얼 보시면 뭐가 빠졌는지 아실겁니다.
여러 데이터베이스 연결하기 http://www.ciboard.co.kr/user_guide/kr/database/connecting.html#connecting-to-multiple-databases