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

2 Answers 2

2

Why would you want to? Why not call it like this?:

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

A complete skeleton of a model is like this:

<?php
class Mymodel extends Model
{
    function get_some_entries($number_rows)
    {
            return $result = $this -> db -> query("SELECT id, name
                                            FROM tablename
                                            LIMIT $number_rows");
    }
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thats better but I just get Array in my view.
@JessMcKenzie: I updated it with a skeleton. Adapt as needed.
I have updated my question but it just seems to give me the id why?
0

Best way? You can probably read on CodeIgniter general guide and alter to your situation. But the basic idea is that, you create the model class then load from your controller. Then just call the model function accordingly. For example,

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

}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.