Welcome Guest, Not a member yet? Register   Sign In
Best practice for model naming and content
#1

[eluser]Andy UK[/eluser]
Can someone tell me what is considered best practice for naming models and what they should contain? Should the model be named after the database table it accesses or should it be related more to the controller?

Up till now, I've been relating the model to the database table, ie. The model has the same name and all access to that table goes through the respective model.

However, now that I'm adding an admin section to the website in question (basically a CMS) I'm thinking it would be nice to have all the admin database access in an admin model, rather than have a mix of admin and non-admin functions in the current model.

Also, I would be able to avoid spreading admin functions across different models as the CMS would have to access various tables in the database.

Thanks!
#2

[eluser]techgnome[/eluser]
Neither... Since models don't usually relate to a single piece of info, but could be made up of data from several sources, call it what it is. After all, you call a car a car right? You don't call it a body_interior_engine_wheels_tranmission_model (at least I hope not!) Call it what it is. Conversely, in theory a single model could be used by multiple controllers, again it doesn't make sense to name the model after the controller.

So in short, call the model what it is. If it's a duck, call it a duck_model, not migratory_bird_model.

-tg
#3

[eluser]tonanbarbarian[/eluser]
i name all of my models after the database tables
and i name my controllers the same way
i have the model as singular and the controller as plural
even though i name my model after the database table i am not that strict about what it does in that model
what i mean is that if the model needs to grab data from another table i will let it, rather than loading another model and get it

as for admin if you want to keep the admin model code separate from the front end model code then create an admin folder in models and put all the admin models in there
#4

[eluser]Andy UK[/eluser]
Thanks guys.

Both valid answers. I've created and admin model for now, but I will consider breaking it up into separate models and placing them in an admin folder should the codebase grow in the future.

For now, most of my models are named after the tables they query and only contain queries relating to that particular table, but I'm thinking of relaxing that methodology a bit. For now I'm left with a mix of both... An admin model which references various tables, with no admin table as such, and then the rest of my models which are named after and reference specific tables in the database.

I'd be interested to hear how others are managing their models. As a newbie it would be nice to follow a best practice from the word go and not have to come back and reorganize everything later!

Andy.
#5

[eluser]John_Betong[/eluser]
I use a text-editor that utilises projects with an alphabetically list of files in the left-hand panel.

By using prefixes all files are listed in group and easier to find.

The prefixes I use are:

c_controller's
m_model's
v_views
_partial_views
 
 
 
#6

[eluser]Vheissu[/eluser]
I tend to name my models by what kind of database table they are addressing or sometimes file they are reading. So for users I have a users_model and for products I have a products_model, etc.




Theme © iAndrew 2016 - Forum software by © MyBB