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

[eluser]zoran119[/eluser]
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?
#2

[eluser]johnpeace[/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]

I think of models as being representative of specific, actual things...like users, or recipes or posts.

A model will typically have:
variables for each field in the database table(s) that my model represents
functions to create/save/delete/load values from the DB into the model
other functions necessary for working with that data

'application' is a bit broad...what does your model 'application' represent? Would it be better broken down into smaller pieces?

You can make variables private and change them with public getters/setters if necessary...but if your code will allow public variables, it's easier to just access the member vars directly.
#3

[eluser]asciiCode[/eluser]
Are there any good examples out there on how to use models specifically? The first two intro videos don't mention them at all.
#4

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...odels.html
#5

[eluser]zoran119[/eluser]
[quote author="johnpeace" date="1283883461"]'application' is a bit broad...what does your model 'application' represent? Would it be better broken down into smaller pieces?[/quote]

'application' is probably ok... think of it as a membership application for a club of some kind.

yeah, i was thinking that the model should represent an entity in my application, rather than being just a 'container' for a whole heap of functions that deal with applications. in my current setup every function takes an application_id (rather than using to $this->application_id) and does whatever it needs to do in the database, which kind of makes the whole object oriented part of mvc useless.

so, in a world more similar to what you have described (model has properties that are the same as the table describing the entity) how would you deal with a query like 'update all applications submitted by bob to approved'. what's a nice way of getting all bob's applications?

i get a bit confused here... i can think of a couple of ways of doing it:

1. having another class called application_set with a private array which contains all the application instances (initialised by an sql statement for example)? and then a function set_status($new_status_id) which would iterate all the object instances and call their set_status method... this seems inefficient... too many database updates.

2. do the same as in 1, but forget the whole idea of a new application_set model/class and just do it all in the controller... but i shouldn't run sql in the controller... where do i put this function to retrieve multiple (bob's) applications?

3. just update it all the poor man's way with sql (update application set status_id = 1 where owner = 'bob')... but then this is not 'clean' and where do i put this sql anyway... doesn't belong in the controller or the application model...

not sure of what the best way would be (probably none of those). any suggestions?
#6

[eluser]asciiCode[/eluser]
Also, do you need to call $this->load->model('Model_name'); in every function of a controller class or can you just call it once from the index or constructor functions and then use it in all of the functions of the controller?
#7

[eluser]zoran119[/eluser]
[quote author="asciiCode" date="1283895247"]Also, do you need to call $this->load->model('Model_name'); in every function of a controller class or can you just call it once from the index or constructor functions and then use it in all of the functions of the controller?[/quote]

i call it in the constructor function of the controller. the code in my post was just for demonstration.
#8

[eluser]zoran119[/eluser]
so no one can tell me how to deal with multiple things (applications in my case)?
#9

[eluser]Nalorin[/eluser]
Models are designed to be an interface for communicating with your database. This way, if your database structure changes, or your needs change, you only have to update the model, rather than all of the calls you were making to your database before.

Wikipedia explains it thus:
Quote:The model is used to manage information and notify observers when that information changes...Domain logic adds meaning to raw data (for example, calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items). When a model changes its state, it notifies its associated views so they can be refreshed.

In my implementation, Models are used for getting and setting data in the database (with a few exceptions), and for preparing such information. In essence, I use it as a method of abstraction, so instead of having to type
Code:
$query_string = "select username,u_id,age,gender from site_user "
        . "where username = \"{$safe_post['username']}\" "
        . "and pass = sha1(\"{$safe_post['password']}\")";

$result = mysql_query($query_string);

if (mysql_num_rows($result) == 1)
{
  $valid_user = mysql_fetch_assoc($result);
  ... prep $valid_user data for use here, and later ...
  ... display members area ...
}
else
{
  $valid_user = FALSE;
  ... display authentication error ...
}
... and then still sometimes needing to manipulate the data more to get it formatted ready for use in the application...

With CodeIgniter, I can instead type
Code:
if ($valid_user = $this->membership_model->authenticate(
        $this->input->post('username'),
        $this->input->post('pass')))
{
  ... display members area ...
}
else
{
  ... display authentication error ...
}
... and zip along, using the data for my application, without very much more work tampering with the data.

I personally use models for prepping then inserting data or for fetching then prepping data so that I could, theoretically, provide someone working on site functionality and serving user requests (working with Controllers) a list of functions in the model, and they would be able to get the data they needed and "hit the ground running."

IMO, models should be named to describe what they are for ("membership_model" for dealing with adding or validating users; "payment_model" for dealing with incoming and outgoing monies; "search_model" for displaying browsing or searching results; and the list goes on...)

Hope that helps!

Cheers,

Neil
#10

[eluser]zoran119[/eluser]
[quote author="Nalorin" date="1283939753"]In my implementation[/quote]

so does your model for a car say look like this:

Code:
class Car extends Model {

    var $car_id = 0;
    var $make   = '';
    var $model = '';
    var $cost = 0;

    function Car()
    {
        // this constructor is not really complete... demo purposes
        parent::Model();
    }
    
    function set_cost($cost)
    {
        $sql = 'update cat set cost = ' . $cost . ' where car_id = ' . $this->car_id;
        mysql_query($sql);
    }

or like this:

Code:
class Car extends Model {

    function Car()
    {
        parent::Model();
    }
    
    function set_cost($car_id, $cost)
    {
        $sql = 'update cat set cost = ' . $cost . ' where car_id = ' . $car_id;
        mysql_query($sql);
    }




Theme © iAndrew 2016 - Forum software by © MyBB