질문 Q #10471
Model호출 에러
유승민 2013-04-20 17:54:56
 안녕하세요 얼마 전에 CodeIgniter를 접하고 이용해서 홈페이지를 만들어보고있는데
메인컨트롤러에서 Model을 한번 호출하고 select 값이 있으면 redirect 해서 서브컨트롤러로 이동하게 해놨는데
서브컨트롤러에서 $this->load->model('notice');  이렇게 모델을 호출 하기만 하면 Fatal error: Cannot redeclare class Notice in Unknown on line 0 이런 에러가 발생합니다.

왜그럴까요! ....?

redirect('http://주소/notice', 'location'); 이렇게 메인컨트롤러에서 리다이렉트 시켜주구요...

서브컨트롤러에서는
$this->load->model('notice');
$this->load->view('notice/notice_L'); //tmpPage
이게 답니다.. 모델 호출부분을 주석처리하면 잘 되고요...
모델 호출부분을
public function __construct()
{
parent::__construct();
// 생성자 코드에 기능추가
}
여기로 옮겨도 똑같은 에러가 발생하구요.. ㅠ
답변 11
#1 꾸숑 2013-04-20 18:39:10
혹시나 도움 될지 해서 댓글 남기네요

콘트롤러에서는 아래 형태로 해야 하지 않을까 생각되네요..

$this->load->model('모델명');//먼저 parent::__construct(); 에서 모델을 로드한후에

//컨트롤러 함수에서 아래 형태로...
 function 컨트롤러함수명(){
$data = $this->모델명->모델함수명();
$this->load->view('notice/notice_L',$data);
}


전체 소스를 올려 주시면 테스트 후에 정확한 답변을 드릴수 있을것 같습니다.

즐거운 휴일 되세요..
#2 유승민 2013-04-21 00:12:58
꾸숑 //

모델을 construct(); 부분으로 옮겨서 호출해도 동일한 에러가 발생합니다... ㅜㅜ
모델 호출 하는 부분에서 저 에러가 발생하더라구요... 저부분 주석처리하고 뷰 호출만 하면 정상 작동하구요..
#3 꾸숑 2013-04-21 00:16:45
유승민//
그러면
$this->load->model('notice');
이게 문제 인것 같네요..
모델쪽 살펴 보세요 분명 뭔가 잘못된것이 있을것 같네요..

모델 소스 올려 보세요... 콘트롤러도 같이요
함 같이 고민해 보자고요..

에러 메세지 번역하면 아래와 같네요..
치명적인 오류 : 라인 0에 알 수없는 notice클래스를 다시 선언 할 수 없습니다
#4 유승민 2013-04-21 11:40:38
꾸숑 //

//컨틀롤러
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Notice extends CI_Controller {
 
public function __construct()
{
parent::__construct();
// 생성자 코드에 기능추가
}
public function index()
{
// 로그인 성공 시 띄워줄 화면
$this->load->model('notice');
$this->load->view('notice/notice_L'); //tmpPage
}
}


//모델
<?php 
class Notice extends CI_Model {
 
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
function select(){
$sql = 'SELECT * FROM NOTICE';
return $this->db->query($sql);
}
}
?>
#5 유승민 2013-04-21 13:44:21
 risa //
바꿔도 동일한 에러가 발생하네요 ㅜ
#6 risa 2013-04-21 15:00:36
 베이스 샘플 (2.x 이상 버젼)

// test.php
<?  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
 function Test() {
  parent::__construct();
  $this->load->database();
  $this->load->model('test_model');
  }
 function index(){
   $return_data = $this->test_model->select();
   print_r($return_data);
 }
}
?>
//test_model.php

<?
class Test_model extends CI_Model {
 function __construct(){
  parent::__construct();
 } 
 function select(){
  $query ="select * from test";
  return $this->db->query($query)->result_array();
 }
}         
?>

다시 한번 체크해 보세요.
#7 유승민 2013-04-21 15:39:00
 risa //
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Notice extends CI_Controller {
 
public function __construct()
{
parent::__construct();
// 생성자 코드에 기능추가
$this->load->database();
$this->load->model('notice');
}
public function index()
{
//로그인 성공 시 띄워줄 화면
$retrun_db_data = $this->notice->select();
print_r($retrun_db_data);
//$this->load->view('notice/notice_L'); //tmpPage
}
}



<?php 
class Notice extends CI_Model {
 
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function select(){
$sql = 'SELECT * FROM NOTICE';
return $this->db->query($sql)->result_array();
}
}
?>

동일한 에러 발생합니다 ㅜ
#8 risa 2013-04-21 15:51:26
 컨트롤러와 모델명을 같게 만드신것 아닌가요?

컨트롤러 파일명과 모델 파일명은 달라야 합니다.

문법적으로 보면 notice 라는 객체는 이미 CI 컨트롤러 라는 슈퍼클래스를  상속 받았는데 
다시 notice 에 CI 모델 슈퍼클래스를 상속 시킬려고 한것입니다.

말로 풀면 부모가 이미 있는데 다른 부모가 유전자를 나눠주겠다는 이야기?
//notice.php   컨트롤러
class Notice extends CI_Controller {

//notice.php 모델
class Notice extends CI_Model {
#9 유승민 2013-04-21 17:21:11
 risa //
감사합니다 ㅜㅜㅜㅜㅜ 멍청돋았네요 ㅜ
#10 꾸숑 2013-04-21 22:01:00
유승민//
축하드려요 해결 하셨네요..
전 오늘 CI스터디가 있어서 빠른 답변 못드렸네요 ^^
#11 유승민 2013-04-24 09:52:20
꾸숑 //
꾸숑님도 감사합니다!