CodeIgniter Forums
silly questions. Article_model or Model_article? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: silly questions. Article_model or Model_article? (/showthread.php?tid=20140)

Pages: 1 2


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]searain[/eluser]
1) when you create model, controller classes, do you add the model/controller in the class name? such as class names as Article_controller, Article_model? I don't see why we need do so, if the controllers are in controllers folder and models are in models folders already. Just name the class as Article will be good enough, right? But many CI samples add the model to the class name. Such as Model_article, or M_article etc. Any reasons?

2) Say if you add the model to the class name, do you name it Model_article or Article_model?

Knowing these questions are silly. But I am in the beginning stage of rewriting my own huge non CI code base to CI based codes. If I start to use this name rule, I will stick with it for my CI based code base and for my CI projects. Want to get it "right" from the first place.

Thanks!


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]Dam1an[/eluser]
Although you can calll both a model and controller 'Article' you'll get an error if you try to load them both at the same time, as the class 'Article' will have already been defined, which is why most people add the _model (or some variation) suffix to the model.
In most case you wouldn't add a suffix to the controller, as this is visible to the end user, unless you want to alias it all (I personally modified to the router to let me use _controller as a suffix)

Of course many users will debate if you should have the model and controller both called article in the first place, I personally use singluar for the model and plural for the controller (in most cases)


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]searain[/eluser]
Thanks!


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]jedd[/eluser]
I think long and hard and come up with meaningful names that satisfy these criteria:
o no use of the word model in my Model's name,
o no plurals in either model or controller names.


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]searain[/eluser]
Jedd, so how do you solve the case that if you load controller and model in the same page? how do you name them differently in your own name rule? thanks!


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]JasonS[/eluser]
I used to use singular and plural. Now I use the suffix of _model. I know it is obvious it is a model but I think it makes the code easier to read.

Instead of having a page controller with the following code.

$this->pages->get_all();

I have this.

$this->page_model->get_all();

I just think it is more obvious where the data is coming from.


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]jedd[/eluser]
[quote author="blackhorse66" date="1246332492"]Jedd, so how do you solve the case that if you load controller and model in the same page? how do you name them differently in your own name rule? thanks![/quote]

I don't have a strict 1:1 correlation between controllers and models - most of my models handle multiple tables in my DB. If you spend a few minutes thinking about your model design - what data you are actually modeling - it's fairly straightforward.

I don't do a 1:1 relationship between models and tables either - but I'm surprised that people that do find the collision between controller and model names even harder to avoid. I'd have thought that would make it much easier, as having a controller that matches the name of a database table seems messy or easy to sidestep (or both).


silly questions. Article_model or Model_article? - El Forum - 06-29-2009

[eluser]tonanbarbarian[/eluser]
I follow the pattern that a model is representing a single entity i.e. a blog
A Controller is representing the collection i.e. blogs

Therefore I have my controller class names as plurals of the related models, which are singular. If my controller does not map to a direct model I still try to have the name of the controller a plural to keep it is sync with the other controllers.


silly questions. Article_model or Model_article? - El Forum - 06-30-2009

[eluser]jedd[/eluser]
What do you do with words that are both plural and singular? Do you avoid using such words or just wear the inconsistency?


silly questions. Article_model or Model_article? - El Forum - 06-30-2009

[eluser]Dam1an[/eluser]
[quote author="jedd" date="1246379516"]What do you do with words that are both plural and singular? Do you avoid using such words or just wear the inconsistency?[/quote]

I've never had to deal with that (yet) and can't think of any such cases off the top of my head