2012-09-26 3 views
0

나는 창문에서 뛰어 내리기 전에 누군가 이걸 가지고 나를 도울 수 있기를 바랍니다. 나는이 시간에 몇 시간을 보냈고 내가 뭘 잘못하고 있는지 모른다.모듈에서 모델을로드 할 수없는 이유는 무엇입니까? [codeigniter 2.1.2에서 HMVC 사용]

기본적으로 HMVC를 CodeIgniter 2.1.2에 설치했는데 모든 것이 잘 작동하지만 표준 컨트롤러에서 모델을로드하는 것과 같은 방식으로 모델을로드 할 수 없습니다. 오래된 codeigniter 1.7.1에서는 $ this-> load-> model ('my_model')을 호출하여 간단하게 사용할 수 있지만 지금은 할 수 없다. 내가 설치 한 한

A PHP Error was encountered 
Severity: Notice 
Message: Undefined property: Special_cart::$db 
Filename: core/Model.php 
Line Number: 51 

는 단계별 지침에 따라 :

나는이 오류 모델을로드하기 위해 노력하고있어 매 시간

. modules 폴더 옆에 third_party가 있습니다. 모듈에서 나는 몇 가지 모듈은 다음과 같이 저장 한 :

modules 
--boxes 
----controller 
----models 
----views 

내가 이렇게 내 코드에서 모듈을 호출 :

내 모듈 컨트롤러 코드는 다음과 같습니다
<?=modules::run('boxes/special_cart/index');?> 

:

class Special_cart extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 

    } 





    public function index() 
    { 
     if ($this->session->userdata('cart')) 
     { 
      # get product id's and each quantity 

      $cart_product_list = array_count_values($this->session->userdata('cart')); 

      # get list of product_id 
      $product_list = array_keys($cart_product_list); 

      # get product details 
      $this->load->model('productmodel'); 
      $this->load->model('stockmodel'); 

      $cart_products = $this->productmodel->cart_get_products_details($product_list); 
      $final_cart_array = array(); 

      foreach($cart_products as $cart_product){ 
       $product_stock = $this->stockmodel->view_product_stock($cart_product["id"]); 
       if(empty($product_stock) || $product_stock["UNITS"]<=0) 
        $cart_product["UNITS"] = 0; 
       else{ 
        if($cart_product_list[$cart_product["id_web"]]>$product_stock["UNITS"]) 
         $cart_product["UNITS"] = $product_stock["UNITS"]; 
        else{ 
         $cart_product["UNITS"] = $cart_product_list[$cart_product["id_web"]]; 
        } 
       } 
       $final_cart_array[] = $cart_product; 

      } 

      $refresh_cart_array = array(); 


      foreach($final_cart_array as $cart_product){ 

       for($i=1;$i<=$cart_product["UNITS"];$i++){ 
        $refresh_cart_array[] = $cart_product["id_web"]; 
       } 

      } 


      $this->load->view("special_cart",array(
               'refresh_cart_array'  => $refresh_cart_array, 
               'final_cart_array' => $final_cart_array 
               )); 





       } else { 
       $this->load->view("special_cart",array(
                'refresh_cart_array'  => NULL, 
                'final_cart_array' => NULL 
                )); 

        } 
      } 

} 

나 ' 나는 인터넷에서 발견되는 모든 가능한 해결책을 시도했다. 그들 중 누구도 일하지 않는다. ... 내 문제를 이해하기를 바랍니다.하지만 좀 더 자세한 설명이 필요하면 저에게 물어보십시오. 누구든지 도와 줄 수 있습니까?

답변

0

로드하려는 모델이 db에 연결하려고하지만 데이터베이스 드라이버를 사용할 수없는 것 같습니다. 응용 프로그램에서 데이터베이스 쿼리를 사용하는 경우 데이터베이스 드라이버가 자동으로로드되지 않는 이유는 무엇입니까?

application/config/autoload.php 파일의 "libraries"배열에 "database"를 삽입하기 만하면됩니다. database 자격 증명을 application/config/database.php에 삽입하는 것을 잊지 마십시오.

$autoload['libraries'] = array('database'); 

당신은 단지 하나 개의 모델에서 데이터베이스 연결을해야하는 경우, 데이터베이스 라이브러리에 액세스하기 전에로드합니다.

위 코멘트에 쓴 것을 바탕으로 (당신이 기압하고있는 것처럼 CI_Contoller되지 않음) MX_Controller 클래스를 확장 사용하기

$this->load->database(); 
+0

이 내 설정 파일에있는 것입니다 : $ autoload [ 'libraries'] = 배열 ​​('session', 'database', 'form_validation', 'email', 'redux_auth'); 하나의 모델에서 언급 한 데이터베이스를로드하려고 시도했지만 잘 작동하지 않았습니다./HMVC 코어에 몇 가지 버그가있을 수 있습니까? – Pavel

0

보십시오, 나는 당신이 모듈에서 DB의 새 인스턴스를 만들려고한다는 생각 (chrises comment에 기반하여). 는

그래서처럼 현재의 구조를 업데이트 Special_cart

의 constructor에 그것을 수행

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->database('default'); 
} 

(내 머리 꼭대기에서이 글을 쓰는, 그래서 방법을 확인하고 있습니다) 확인 dB 지금

을 드라이버는 모델에서 사용할 수 있어야합니다.

문제는 HMVC과 관련된 문제는 없습니다.지금은 잠시 동안 HMVC을 사용하고, 나는 (데이터베이스 작업) 거기에 아무런 문제가 발견되지

+0

그러나 자동로드 구성에 삽입 할 때 db 드라이버도로드되지 않았습니까? 그것으로 DB 드라이버는 모델의 생성자에서도 준비가되어 있어야합니다. –

+0

그렇습니다. 그러나 질문에 따라, 그것은 atm 경우가 아닙니다. MX에서로드 할 때 CI가 db 클래스의 인스턴스를 잃어 버리는 경우가 있습니다. 이유는 모르겠지만, 이전 경험에서 그것은 코드 자체가 이전에 생성 된 다른 객체에 대한 포인터를 잃어 버리는 극단적 인 후방 상황입니다. 그래서 데이터베이스를 다시로드하려고 시도하고 도움이 될 것이라고 생각하는 이유입니다. 그러나 첫번째 해결책은 Special_cart에 대한 부모 클래스를 변경하는 것이 었습니다. $ this-> load-> database ('default')는 db 인스턴스가 생성되어 사용 가능하다는 것을 확인한 것입니다. – NemC

0

사용합니다

 
    $this->load->model('module_name/productmodel'); 

0
Class Models extends MX_Loader{ 

    function getUser($username){ 

     $sql="SELECT * FROM user WHERE username = ? "; 
     return $this->db->query($sql,array($username))->row(); 

    } 
} 

이 MX_Loader을 확장 다음과 같이 모듈 이름을 알리는 모델을로드하십시오 CI_Model을 사용하면 Codeigniter에서로드 될 수 없는지 알 수 없기 때문에,

0

같은 문제와 실수가있었습니다. 나는 컨트롤러를 MX_Controller로 확장하지 않았다. 그래서 해결책은 다음과 같이 MX_Controller에 CI_Controller을 변경하는 것입니다 : 이것은 또한 여기에 문서 https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/ 에 견적을 설명

class Special_cart extends MX_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('productmodel'); 
     $this->load->model('stockmodel'); 
    } 

    public function index() 
    { 
     if ($this->session->userdata('cart')) 
     { 
      # get product id's and each quantity 

      $cart_product_list = array_count_values($this->session->userdata('cart')); 

      # get list of product_id 
      $product_list = array_keys($cart_product_list); 

      # get product details 

      $cart_products = $this->productmodel->cart_get_products_details($product_list); 
      $final_cart_array = array(); 

      foreach($cart_products as $cart_product){ 
       $product_stock = $this->stockmodel->view_product_stock($cart_product["id"]); 
       if(empty($product_stock) || $product_stock["UNITS"]<=0) 
        $cart_product["UNITS"] = 0; 
       else{ 
        if($cart_product_list[$cart_product["id_web"]]>$product_stock["UNITS"]) 
         $cart_product["UNITS"] = $product_stock["UNITS"]; 
        else{ 
         $cart_product["UNITS"] = $cart_product_list[$cart_product["id_web"]]; 
        } 
       } 
       $final_cart_array[] = $cart_product; 

      } 

      $refresh_cart_array = array(); 


      foreach($final_cart_array as $cart_product){ 

       for($i=1;$i<=$cart_product["UNITS"];$i++){ 
        $refresh_cart_array[] = $cart_product["id_web"]; 
       } 

      } 


      $this->load->view("special_cart",array(
               'refresh_cart_array'  => $refresh_cart_array, 
               'final_cart_array' => $final_cart_array 
               )); 





       } else { 
       $this->load->view("special_cart",array(
                'refresh_cart_array'  => NULL, 
                'final_cart_array' => NULL 
                )); 

        } 
      } 
} 

:

Notes:

To use HMVC functionality, such as Modules::run(), controllers must extend the MX_Controller class. To use Modular Separation only, without HMVC, controllers will extend the CodeIgniter Controller class. You must use PHP5 style constructors in your controllers. ie:

<?php 
class Xyz extends MX_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 
    } 
} 
관련 문제