Welcome Guest, Not a member yet? Register   Sign In
model to model function calling?
#1

[eluser]delay[/eluser]
I have a general_model in which I keep various string and date manipulation functions. I want to call the general_model functions within other models.

For instance I have a search_model and it returns search results. One general function I use limits the description text to a few words, I keep this function in my general_model since its useful in other areas as well.

I haven't figured the syntax for calling on one model from another model. Is this doable? If not, what is the correct way to handle what I want to do?

Thanks for any help.
#2

[eluser]gon[/eluser]
You must get a reference to CI object, this way:

Code:
$CI =& get_instance();

Then, $CI is just the same as $this when in controllers.

So you can do:

Code:
$CI->load->model('another_model');
$CI->another_model->whatever_method();

Usually you will want to store the reference into an instance property, so you can access it from any method of the model:

Code:
$this->CI =& get_instance();

That said, common utiliy functions are stored in libraries. So you can write a Utils library, and load it from the models:

Code:
class Example_model extends Model {
    function Example_model() {
        parent::Model();
        $this->CI =& get_instance();
        $this->load->library('utils');
    }

    function example_method() {
        $current_date = $this->CI->utils->get_nicely_formatted_date();
        .......
    }
}
#3

[eluser]Randy Casburn[/eluser]
Hi delay,

If ...
Quote:I have a general_model in which I keep various string and date manipulation functions.
is your only goal, then the true CI solution is called a Helper. You should put these simple procedural calls inside a Helper file and keep thing efficient. All the extra overhead mentioned above may not be necessary.

Besides, helpers already exists for Dates:

http://ellislab.com/codeigniter/user-gui...elper.html

and for string stuff:

http://ellislab.com/codeigniter/user-gui...elper.html

http://ellislab.com/codeigniter/user-gui...elper.html

http://ellislab.com/codeigniter/user-gui...elper.html

http://ellislab.com/codeigniter/user-gui...elper.html

Just in case you haven't seen them.

Randy
#4

[eluser]delay[/eluser]
Thanks gon and Randy,

I think is what I need to do is to extend one of the existing helpers. The CI built-in ones don't do exactly what I need done and I do want to kind of do it in the CI correct way, since I am trying to learn the ins and outs of the framework anyway.

Thanks for both of your input, it was very helpful.
#5

[eluser]Colin Williams[/eluser]
Yeah, the model concept can be finicky, because anytime you are formating data, you are essentially modeling it. However, it seems best to reserve Models for external data calls and formatting, like databases or web services. A helper or library seems the way to go. I would only go with a library if it makes sense for it to be object-oriented.




Theme © iAndrew 2016 - Forum software by © MyBB