CI 묻고 답하기

제목 모델 load 시 파라메터의 차이를 아시는분...
글쓴이 신개발자 작성시각 2013/11/27 13:09:40
댓글 : 7 추천 : 0 스크랩 : 0 조회수 : 12911   RSS
 
1> $$this->load->model('모델명'); 
방식으로 하고 
모델.php 의 __construct 에서 database를 로드 하는 방식으로 넣고
있습니다.

function __construct()
    {
        // Call the Model constructor
        parent::__construct();
$this->db = $this->load->database('DB명', TRUE);
    }
 
select 를 할땐 1번으로 가능한데 insert 또는 update 는 1번 방식으로 안되더라구요.

그 후 $this->load->model('모델명' , '' , 'DB명'); 

으로 db를 load 하니까 insert 가 되던데, 이 두가지 방식의 차이가 몬지 아시는분 계신가요???
 다음글 로그인 페이지를 만들려고 합니다! (2)
 이전글 application 폴더를 그대로 카피해서 쓰는데.. (3)

댓글

변종원(웅파) / 2013/11/27 13:35:29 / 추천 0
질문에 답이 있는데요? 모델 로딩할때 파라미터 넘겨서 그 db설정을 여는겁니다. config/databases.php 열어보세요
신개발자 / 2013/11/27 14:10:33 / 추천 0
그런데 모델 파일 생성자에서 이미 db 설정을 열었어요,
function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->db = $this->load->database('DB명', TRUE);
    }

이상태에서  
$$this->load->model('모델명');  
식으로 모델명을 로드 하고 selct 를 하면 이상이 없는데 
insert나 update 할
 때 

$this->db->insert('todo_person', $data);

이 부분에서 
codeigniter call to undefined method ci_db_mysql_driver::insert()

라는 에러가 떨어지더라구요.

insert 를 할때에는 모델을 로드할 때 db설정을 또 열어줘야하는건가요?




변종원(웅파) / 2013/11/27 14:37:08 / 추천 0
insert, update 모델은 동일한 모델파일안에 있는건가요?
아니면 다른 모델에?

소스를 올려주시면 원인파악이 빠를 것 같네요.

db 라이브러리 로드가 안되서 그런걸겁니다.
신개발자 / 2013/11/27 17:16:47 / 추천 0
test.php
require(APPPATH.'libraries/REST_Controller.php');
class Test extends REST_Controller
{
// Constructor function
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function classifyList_get()
{
$idx = $this->get('idx');
$this->load->model('Test_Model');
$result = array();
$result = $this->Test_Model->classifyList_get($idx);
if ($result){
$res = array('success'=>true,'result'=>$result);
}
else {
$res = array('success'=>false,'error'=>'오류.');
}
$this->output($res);
}
public function saveNewList_post()
{
$this->load->model('Test_Model','','dev');
$priority = $this->post('priority');
$list_idx = $this->Test_Model->insert_todoList($priority);
if ($result){
$res = array('success'=>true,'result'=>$result);
}
else {
$res = array('success'=>false,'error'=>'오류.');
}
$this->output($res);
}
}
 
 
test_model.php
class Test_Model extends CI_Model
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
 
$this->db = $this->load->database('dev', TRUE);
    }
function insert_todoList($priority){
 
$data = array(
'priority' => $priority
);
$this->db->insert('test_list', $data);   // 이부분에서 codeigniter call to undefined method ci_db_mysql_driver::insert() 가 생깁니다.

$last = $this->db->insert_id();
return $last;
}
function classifyList_get($idx){
$sql = ";
";
$query = $this->db->query($sql);
return $query->result_array();
}
}

보시면 test_model 생성자에서 database 를 접근하게 햇거든요..

위 소스에서 select 를 할때 
1>    $this->load->model('Test_Model'); 
경우가 상관이없는데 insert 하는부분에  1>을 넣어주면 에러가 떨어지더라구요.

1>    $this->load->model('Test_Model'); 
2>    $this->load->model('Test_Model','','dev'); 

1번의 경우는 select 만 가능한것인지 , 아니면 제가 소스상에 문제가 있는건지 모르겠네요.
2번의 경우는 정상적으로 되구요.

 
변종원(웅파) / 2013/11/27 17:51:45 / 추천 0
ci가 아닙니다. 당연히 ci의 라이브러리를 사용못하죠

require(APPPATH.'libraries/REST_Controller.php');
class Test extends REST_Controller


매뉴얼에서 컨트롤러 어떻게 선언하는지 보세요.
저렇게 외부 rest 클래스를 로딩하면 당연히 ci의 내부 라이브러리를 사용할 수 없습니다.

작동되는게 신기하네요. (ci가 너무 느슨한듯...)
한대승(불의회상) / 2013/11/27 18:01:05 / 추천 0
REST_Controller class가 CI_Controller를 상속받았다면 사용 가능하실겁니다. ^^
신개발자 / 2013/11/27 18:17:09 / 추천 0
아 감사합니다 ㅠㅠ 좀더 꼼꼼히 봤으면 될 문제였군요...ㅎㅎ