Question

Hello fellows can someone help me into this:

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

I'm confused on how naming convention on Models in cakephp.

I've noticed that in the blog tutorial the model is named "Post" which is singular

class Post extends AppModel {
}

while in the controller the "Post" is "Posts" which is plural:

class PostsController extends AppController {
    public $helpers = array('Html', 'Form');
}

Can someone explain this to me, thanks.

Was it helpful?

Solution

It's annoying, I know.

By default Cake uses pluralization on table names and controllers, but not on models. See here for more info.
I made a decision early on to do away with this convention, and just go with the singular name everywhere.

I don't recommend doing this without doing a lot of research first, as it breaks the CakePHP convention and you then need to be aware of what's going on.

If you're sure that pluralization is more of a hindrance than a help, put this into your bootstrap.php file:

// Turn off pluralization altogether
Inflector::rules(
    'plural', 
    array(
        'rules' => array('/^([a-zA-Z_-]*)$/i' => '\1'), 
        'irregular' => array(), 
        'uninflected' => array()
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top