CodeIgniter Forums
How to use models? - 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: How to use models? (/thread-33743.html)

Pages: 1 2


How to use models? - El Forum - 09-08-2010

[eluser]Buso[/eluser]
[quote author="zoran119" date="1283870019"]hi all,

i was hoping someone can tell me what is the correct way to use models.

i have a model called 'application' extending the ci model class, but i just have a bunch of functions there which get used in the controller called 'application'. this model does not have any private variables. for example, there is a function update_status($application_id, $new_status_id) which i call from the controller like this

Code:
$this->load->model('application');
$this->application->update_status($application_id_to_update, $new_status_id);

i'm thinking that this is not the way to do things. should the model be used as a proper class, with properties and methods? so for the example above, should the application model have a private property called status_id and then implement a method called set_status_id($new_status_id) to set the private property and another method called save() which would save the change to the database?

i'm thinking it should be done the second way, just not sure. anyone got an opinion?[/quote]
Both approaches are commonly used.

Personally, I prefer the first one (but suffix your models with '_m' or '_model' so you don't have collisions with say, libraries), but if you like the second one you can try DMZ out. It's an ORM for Codeigniter.

Oh and remember that models are supposed to handle your business logic in general, not only database-related stuff.


How to use models? - El Forum - 09-08-2010

[eluser]Nalorin[/eluser]
@zoran119

Depending on what you're trying to accomplish, either one of the examples you provided could be useful, but both could use some improvement.

Here's a basic car model:
Code:
class Ferrari extends Model
{
  // controller is only needed if you need to do work when the model starts up
  function update_cost($car_id, $cost)
  {
    $data = array ( 'cost' => $cost );
    $this->db->where('car_id',$car_id);
    $this->db->update('ferrari_table',$data);
  }
}

If you'll be getting information from the model several times during a single page load, then the model can become more versatile because you can store information in model members if you want to.

From what I understand, the purpose of Models is to separate the database functionality from the rest of your code.

~~~ Edit ~~~
You might add to the one example above as follows:
Code:
function check_inventory($model)
  {
    $this->db->select('count(*) as inventory_level');
    $this->db->like('model',$model);
    $query = $this->db->get('ferrari_table');
    return $query->result();
  }
~~~ /Edit ~~~
If you want a good tutorial on using Models: CodeIgniter from Scratch: Day 6 - Login, from Tuts+. That tutorial and the other tutorials from that series are really helpful. MOst of them are also available free on iTunes.

Cheers,