Welcome Guest, Not a member yet? Register   Sign In
How to use models?
#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


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