![]() |
Do I use a model, library or helper? - 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: Do I use a model, library or helper? (/thread-55255.html) |
Do I use a model, library or helper? - El Forum - 10-17-2012 [eluser]Lpeek[/eluser] Hi all! I'm just trying to figure out where things should be going in my file structure. I have a controller which, for example, would be 'category' which will show a set of products in a category. But what would I do to create the products? Before CI, I would have created a product.php class and put it simply in a classes folder. I would then loop through my products and create a new instance of the product class for each and do whatever I need with it with $product->title etc. I'm wondering whether in CI would I use a model, a helper, or would I create a product.php in libraries? And when I load, would I simply call: Code: $this->load->library('product'); The class would include all the vars for the product (title, price, description etc), as well as functions related to individual products. Cheers! Do I use a model, library or helper? - El Forum - 10-17-2012 [eluser]egy_programozo[/eluser] Create a Model, and use like this: Code: $query = $this->db->query("SELECT * FROM users;"); Do I use a model, library or helper? - El Forum - 10-18-2012 [eluser]Lpeek[/eluser] Ok Thanks! So I'd use a model, are the attributes from the database automatically assigned to the model class? so I dont have to do any fetching of values from the row itself? Do I use a model, library or helper? - El Forum - 10-18-2012 [eluser]noslen1[/eluser] Models are intended to be used for manipulating data in your database. If you want to create an object class with variables and functions, I think you should create a library like so : /libraries/Product.php Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); You can use also a Model as mentionned by egy_programozo to retrieve data from database, for instance : /models/products_model.php Code: <?php And call both into your controller : /controllers/products.php Code: $this->load->model('products_model'); This would be a bit cleaner I guess, but more code as well. Do I use a model, library or helper? - El Forum - 10-18-2012 [eluser]Lpeek[/eluser] thanks noslen! Looks good! Although it is confusing me as to why this is a good idea? Why is it better to have 2 separate files for the product data, and the product manipulation in the database? I've always used functions and variables in one class for a certain object like a product, and it has never occurred to me that it would be better separated. It just makes it awkward, instead of creating a product object, and running everything I need on that object, first I have to load the model to get info from the database, I then pass that to the library to use, when I need to go back to the database then go back to the model, then pass that info back to the library again. It just seems like a lot of pointless backwards & forwards? There must be a good reason? It's not as obvious as views are for splitting logic from layout. Do I use a model, library or helper? - El Forum - 10-18-2012 [eluser]noslen1[/eluser] Views are for layouts as Models are for database. You wouldn't write a function into your view that does not display anything, right ? Well, I don't use model to do anything else but database manipulation, that's all, but that's my logic. Everyone can do what he wants to, unless it is logic enough for him. You could develop a whole website into a single view, or a controller or whatever you want. I prefer to separate each part to what it is intended for. Do I use a model, library or helper? - El Forum - 10-18-2012 [eluser]Lpeek[/eluser] I think I worded my reply wrong ![]() Do I use a model, library or helper? - El Forum - 10-18-2012 [eluser]noslen1[/eluser] You're right, the fact is that using just a Model with one function to retrieve data from DB, and twenty functions to make some operations on those data, away from DB is kinda awkward to me also. My models are just used for creating, retrieving, updating or deleting data from DB. But I don't know what you exactly wanna do with your object. So maybe Model sounds better for your case. |