0

What is the best way to create my model so I can call it from my controller like this?

$this->model_model->function->dbname

I have the following model but its rubbish:

Model:

function systemName()
{

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

    return $query->result();
}

Update:

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

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

    return $query->num_rows();
}
4

2 に答える 2

2

なぜあなたはしたいですか?なぜこのように呼ばないのですか?:

 $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");
    }
}
?>
于 2012-06-12T04:29:08.980 に答える
0

最良の方法?おそらく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);
}

}
?>
于 2012-06-12T04:33:37.083 に答える