2013-03-03 40 views
0

這使我瘋狂......Jamie的Codeigniter MY_Model

我有CI 2.1.3的全新安裝。
複製MY_Model從這裏:https://github.com/jamierumbelow/codeigniter-base-model 到application/core。
自動加載數據庫autoload.php
配置的config文件夾內爲database.php正常。

擴展的MY_Model類象下面這樣:

class User_m extends MY_Model{ 

    public $_table = 'user'; 
    public $primary_key = 'user_id'; 

} 

而且在默認控制器:

$this->load->model('user_m', 'user'); 

$row = $this->user->get(1); 
echo $row->email; 

這是我怎麼看的CRUD LIB作品,但得到以下錯誤最簡單的實現:

Fatal error: Call to a member function where() on a non-object in MY_Model.php on line 135 

線MY_Model.php 135:

$row = $this->_database->where($this->primary_key, $primary_value) 
         ->get($this->_table) 
         ->{$this->_return_type()}(); 
+0

嘗試它而不重命名模型:'$ this-> user_m-> get(1);' - 可能是擴展模型不支持重命名。 – swatkins 2013-03-03 14:42:10

+0

試過了。沒有運氣.. – mallix 2013-03-03 14:44:48

+0

該類的_set_database函數一定有問題。我不知道爲什麼那個函數在那裏,因爲你可以從CI配置文件加載數據庫。 – mallix 2013-03-03 15:10:58

回答

1
if (!$this->_db) 
{ 
    $this->_database = $this->load->database(); 
} 

沒有數據庫對象將從$這個 - >負載>數據庫()返回;

從CI文檔:

/** 
* Database Loader 
* 
* @param mixed $params  Database configuration options 
* @param bool $return  Whether to return the database object 
* @param bool $query_builder Whether to enable Query Builder 
*     (overrides the configuration setting) 
* 
* @return void|object|bool Database object if $return is set to TRUE, 
*     FALSE on failure, void in any other case 
*/ 
public function database($params = '', $return = FALSE, $query_builder = NULL) 

嘗試:

if ($this->db) { 
    $this->_database = $this->db; 
} else { 
    $this->_database = $this->load->database('default', true, true); 
} 

後來編輯:
我發現模型的一個固定的版本。替換你的核心模型this one

+0

正是我認爲的問題。謝謝 – mallix 2013-03-03 15:51:37

相關問題