创建
模型以便我可以像这样从控制器调用它的最佳方法是什么?
$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();
}
你为什么要这样做? 为什么不这样称呼它?
$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");
}
}
?>
最好的方法?您可能可以阅读CodeIgniter一般指南并根据您的情况进行更改。但基本思想是,创建模型类,然后从控制器加载。然后只需相应地调用模型函数。例如
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);
}
}
?>