Welcome Guest, Not a member yet? Register   Sign In
How to use models?
#12

[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,


Messages In This Thread
How to use models? - by El Forum - 09-07-2010, 03:33 AM
How to use models? - by El Forum - 09-07-2010, 07:17 AM
How to use models? - by El Forum - 09-07-2010, 07:51 AM
How to use models? - by El Forum - 09-07-2010, 07:52 AM
How to use models? - by El Forum - 09-07-2010, 07:59 AM
How to use models? - by El Forum - 09-07-2010, 10:34 AM
How to use models? - by El Forum - 09-07-2010, 05:15 PM
How to use models? - by El Forum - 09-07-2010, 05:16 PM
How to use models? - by El Forum - 09-07-2010, 10:55 PM
How to use models? - by El Forum - 09-08-2010, 02:46 AM
How to use models? - by El Forum - 09-08-2010, 02:55 AM
How to use models? - by El Forum - 09-08-2010, 07:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB