Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: when to use a model vs library?

Tags:

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

like image 790
wag2639 Avatar asked Jun 24 '10 19:06

wag2639


People also ask

What is the difference between model and library?

Libraries are tool collections and function extensions while Models are natively ideal for abstracting and interacting with data coming from databases.

What is the use of model in CodeIgniter?

In CodeIgniter Model are the PHP classes where all database related manipulation is done e.g. fetching records, insert, update, and delete records. Within this, all data processing logic is done. All model files are managed in application/models directory and they are load and access by the controller.

Is CodeIgniter a framework or library?

CodeIgniter is an Application Framework Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries.

Can we load library in model CodeIgniter?

Loading the Custom LibraryUse the name of the library without “. php” extension. After loading the library, you can also call the function of that class as shown below.


2 Answers

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.

like image 94
BoltClock Avatar answered Sep 30 '22 09:09

BoltClock


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.

like image 36
Bella Avatar answered Sep 30 '22 07:09

Bella