2012-09-26 52 views
0

我希望有人能幫助我這個人之前我跳下窗口。我花了幾個小時在這一個,不知道我在做什麼錯。爲什麼我無法在模塊中加載模型? [在笨2.1.2使用HMVC]

基本上,我在笨2.1.2安裝HMVC,一切工作正常,但由於某種原因無法加載模型我做它的標準控制器相同的方式。在舊的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 

我已經安裝了它按照指示一步一步。我在模塊文件夾旁邊獲得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

貌似你試圖負載要連接到數據庫的模型,但數據庫驅動程序不可用。如果在應用程序中使用數據庫查詢,爲什麼不自動加載數據庫驅動程序?

中的application/config/autoload.php文件中的 「庫」 數組中只需插入 「數據庫」。不要忘記將你的數據庫憑證插入到application/config/database.php中。

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

如果您只需在一個模型中建立數據庫連接,請在嘗試訪問數據庫庫之前加載它。

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

這是我在我的配置文件:$ autoload ['libraries'] = array('session','database','form_validation','email','redux_auth');.我也嘗試加載你在單個模型中提到的數據庫,但是沒有工作:/ HMVC內核中可能存在一些錯誤? – Pavel

0

嘗試使用擴展MX_Controller類(不CI_Contoller像你大氣壓做)根據你已經在評論上面寫道

,我想你試圖模塊來創建數據庫的新實例(基於chrises評論)。 做它Special_cart

的constuctor

所以更新當前構建要像

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

(我是從我的頭頂寫這個,所以檢查的方法)

現在肯定分貝驅動程序應該在您的模型中可用。

關於與HMVC我不認爲有任何問題。我使用HMVC有一段時間了,我沒有發現任何問題,它(使用數據庫)

+0

但是,當插入到自動加載配置中時,db驅動程序是否也沒有被加載?有了這個,數據庫驅動程序應該準備好,即使在模型的構造函數中,不應該嗎? –

+0

它應該,是的。但基於問題,情況並非如此。 由於某些原因,CI從MX加載時丟失了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。因此,解決辦法是將是CI_Controller改變MX_Controller這樣的:

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 
                )); 

        } 
      } 
} 

這是文檔 https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/ ,在這裏也說明了報價:

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(); 
    } 
}