Welcome Guest, Not a member yet? Register   Sign In
Loading a model in a model - is bad?
#1

[eluser]DanSearle[/eluser]
I thought I was going ok until I find I can't easily load a model from within a model, and a Google search turns up that it's not the "right way" anyway. So I'm not sure the best way to go about this.. here's my situation (as best as I can explain it without revealing project details):

I have a model to handle CRUD operations on a user table. When a user is modified or created I want to send an email (done with a helper). Also there is a certain type of user that when updated causes updates in a 2nd table - this is handled in a second model.
Since there will be various points in my app where these operations can be used, I wanted to move as much functionality into the model as possible to avoid duplication and to simplify my controllers - initially I was doing all this in the first controller I made where it was all fine.

So where would it be best to place the code that accesses the 2nd model?

Many thanks,
Dan
#2

[eluser]DanSearle[/eluser]
turns out the helper doesn't work from the model either.

So is it better to create a new library class and put all this common code in there that accesses models and helpers, to be used from my Controllers?
#3

[eluser]CroNiX[/eluser]
You can load more than one model in a controller ya know Smile
#4

[eluser]DanSearle[/eluser]
well yeah, I know that much.

My situation is that when a Create action happens on my user table, it also causes a Create action on a second table, and it also generates an email. Since these actions always happen on Create - I thought the smart place to put all this code would be in one model - but I see that's breaking the MVC pattern so I'm looking for alternatives.
I don't want to put all the code into the controller, because there will be more than one controller making these Create actions, and I don't want to duplicate this code.

So I think now the best place to put it all is in a Library class - this lib can have a public "create" method - my controllers call that, and this create method does all the updating to models and email sending.

I'm new to this and I'm just trying to go about things the way CodeIgniter intended me to, rather than hack in a solution.
#5

[eluser]eoinmcg[/eluser]
from a best practises point of view, you're on the right track with the library approach
#6

[eluser]DanSearle[/eluser]
thanks, so far it's going well.
#7

[eluser]Vladimir Aleksiev[/eluser]
You can load second model, using an instance of CodeIgniter class.
Code is something like this:
Code:
$CI = & get_instance();
$CI->load->model('my_second_model');
$this->second_model = $CI->my_second_model;
$this->second_model->do_what_you_should_do();
#8

[eluser]jedd[/eluser]
Hi Dan,

Two other approaches you may want to consider:
1. do both table writes in the same model (you don't need a separate model for every table),
2. implement a MY_Model (extend the model class).

There is some info in the wiki about extending [url="/wiki/MY_Controller"]MY_Controller[/url] and a few pointers in there to extending other base classes, plus you can search the wiki && forums for MY_Model.
#9

[eluser]Mario Rojas[/eluser]
You must to load this models in a controller




Theme © iAndrew 2016 - Forum software by © MyBB