Welcome Guest, Not a member yet? Register   Sign In
Best practce for calling functions between models
#1

[eluser]mact1079[/eluser]
Since I'm pretty new to MVC frameworks...

I have a Users_model function addUser which inserts a user record into the User table based on the info they inputted in the registration form.

I also have a Invites_model functions createInvite and addInvitewhich creates and inserts an invitation record into the Invitations table for the same user.

I want all this to happen when the user creates an account.

Is it ok for me to call the Invites_model function from the Users_model to complete both table inserts?
#2

[eluser]TWP Marketing[/eluser]
So long as you load both models in your controller or by the autoload file. A model is just another class to CI, so no harm, no foul.
#3

[eluser]PhilTem[/eluser]
I'd suggest using libraries in this case maybe a library called 'registration' that takes care of all stuff that has to be done on user registration.

And that's also what libraries are most used for: More than one model necessary then use a library Wink
#4

[eluser]mact1079[/eluser]
Hmm ok, I guess two different opinions there. Is that the best practice rule to apply? If I have to use more than one model then I should move those functions to a class in the libraries folder and do it all there? Or do you have any other rules of when to use libraries to guide me?

http://ellislab.com/codeigniter/user-gui...aries.html is useful in telling me how to use custom or native libraries but I'd like to make sure I'm using them appropriately.

Thanks very much. I am really appreciating the community support for CI as someone who is new to it. Don't know where I'd be without you guys! or gals!
#5

[eluser]johnpeace[/eluser]
A library is really just a class. If you have several models to interact with, it would probably be easier/better to encapsulate those functions into a library that loads the appropriate models and interacts with them there. No reason you can't load a model within another model and call it's methods...but a library is probably more 'best practice'
#6

[eluser]Aken[/eluser]
The two solutions here are really just a matter of coding preference. There's nothing wrong with either one. Use the one that makes more sense to you and anyone else that has to deal with the code.

You can load models inside other models, btw. Nothing wrong with that. That's the way I would handle it.




Theme © iAndrew 2016 - Forum software by © MyBB