wag2639
wag2639

Reputation: 2583

Codeigniter: when to use a model vs library?

I've starting using Codeigniter for a project recently (few months ago) but its getting a little out of hand with a bunch of models that need to interact with each other and I was wondering if I should be creating a library instead?

In my case, I have a user action that happens when you win a game and it would get logged in my user_model but I also want it to get put in my events_model?

Should something like this that affects multiple models become a library?

I know it should NOT be in the controller because I'd have to reuse this trigger in multiple controllers (might not make sense for the provided example but does for my application).

Upvotes: 20

Views: 5619

Answers (2)

BoltClock
BoltClock

Reputation: 723729

Libraries are meant to provide reusable functionality for your application, while models in the MVC architecture represent the data logic in or the actual data used by your application.

So to answer your question, you can implement a library that handles updating of multiple models. Just ensure that things like database inserts are handled by your models themselves, as that is business logic more than application logic.

Upvotes: 23

Bella
Bella

Reputation: 3217

You could leave your models modularised as they are and write your logic in libraries, this way you can access mutilple models though a single function in a library, without making your controllers untidy by loading multiple models and having none-interface orientated logic mingled in with interface logic.

Upvotes: 3

Related Questions