• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do I use a model, library or helper?

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:
which would replace $this->product each time I load it?

The class would include all the vars for the product (title, price, description etc), as well as functions related to individual products.


Create a Model, and use like this:
$query = $this->db->query("SELECT * FROM users;");

foreach ($query->result('User') as $row)
   echo $row->name; // call attributes
   echo $row->reverse_name(); // or methods defined on the 'User' class

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?

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 :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product {
    var $title = '';
    var $price = 0;
    var $description = '';

    public function __construct(){
        // Constructor function called each time you create a new product
    public function calculate_new_price(){

You can use also a Model as mentionned by egy_programozo to retrieve data from database, for instance :
class Products_model extends CI_Model{
    function get($id)  {
        $this->query = $this->db->select('title, price, description')
            ->where('id', $id)

        return $this->query->row();

And call both into your controller :

// Retrieve database data from your model
$my_product = $this->products_model->get($product_id);
// Set your data to your object variables
$this->product->title = $my_product->title
$this->product->price = $my_product->price
$this->product->description = $my_product->description
// Call a function to do what you want
$this->product->price = $this->product->calculate_new_price();

This would be a bit cleaner I guess, but more code as well.

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.

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.

I think I worded my reply wrong Smile I didn't mean everything should be in a view, I understand why we have controllers and views etc. I meant that the reason to separate models and libraries isn't as obvious as between controllers and views as it just seemed to make using objects such as products/users etc more awkward.

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.