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


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