2012-06-12 42 views
0

什麼是創建我的模型的最佳方式,所以我可以從我的控制器這樣調用它?Codeigniter:數據庫簡單回聲

$this->model_model->function->dbname

我有以下的模型,但它的垃圾:

型號:

function systemName() 
{ 

    $query = $this->db->query("SELECT cms_name FROM options"); 

    return $query->result(); 
} 

更新:

function systemOptions($options) 
{ 
    $this->db->select($options); 

    $query = $this->db->get('options'); 

    return $query->num_rows(); 
} 

回答

2

爲什麼WOU你想要嗎?爲什麼不這樣稱呼它?:

$this->model_model->functionname('dbname'); 

模型的完整的骨架是這樣的:

<?php 
class Mymodel extends Model 
{ 
    function get_some_entries($number_rows) 
    { 
      return $result = $this -> db -> query("SELECT id, name 
              FROM tablename 
              LIMIT $number_rows"); 
    } 
} 
?> 
+0

這更好,但我只是在我看來陣列。 –

+0

@JessMcKenzie:我用骨架更新了它。根據需要調整。 – wallyk

+0

我已經更新了我的問題,但它似乎只給了我爲什麼? –

0

最佳方式嗎?您大概可以閱讀CodeIgniter general guide並改變您的情況。但基本的想法是,你創建模型類,然後從你的控制器加載。然後相應地調用模型函數。例如,

class Custom_model extends CI_Model { 


function __construct() 
{ 
    parent::__construct(); 
} 

function systemName() 
{ 

    $query = $this->db->query("SELECT cms_name FROM options"); 
    return $query->result(); 
} 

... 
... 

function systemOptions($options) 
{ 
    $this->db->select($options); 

    $query = $this->db->get('options'); 

    return $query->num_rows(); 
} 


} 

<?php 
class CustomController extends CI_Controller { 

public function __construct() 
{ 
    parent::__construct();  
    $this->load->model('custom_model', 'fubar'); 
} 

public function index() 
{ 
    $result = $this->fubar->systemName('dbname'); 
    print_r ($result); 
} 

} 
?>