CodeIgniter Forums
model to model function calling? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: model to model function calling? (/thread-10942.html)



model to model function calling? - El Forum - 08-19-2008

[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.


model to model function calling? - El Forum - 08-19-2008

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



model to model function calling? - El Forum - 08-19-2008

[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-guide/helpers/date_helper.html

and for string stuff:

http://ellislab.com/codeigniter/user-guide/helpers/string_helper.html

http://ellislab.com/codeigniter/user-guide/helpers/typography_helper.html

http://ellislab.com/codeigniter/user-guide/helpers/text_helper.html

http://ellislab.com/codeigniter/user-guide/helpers/inflector_helper.html

Just in case you haven't seen them.

Randy


model to model function calling? - El Forum - 08-19-2008

[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.


model to model function calling? - El Forum - 08-19-2008

[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.