What’s the benefit of the singular/plural conventions in CakePHP?

Published on and tagged with cakephp  convention  ideas

While reading the CakePHP books I recently reviewed here, a question emerged for which I don’t have an answer: what’s the benefit of the singular/plural conventions we have in CakePHP for me as a framework user?

With singular/plural conventions I mean the following conventions every new baker has to learn while doing his first steps with Cake:

  • Table names are plural (e.g. posts, categories, request_tokens)
  • Model names are singular (e.g. Post, Category, RequestToken)
  • Controller names are plural and ending with “Controller” (e.g. PostsController, CategoriesController, RequestTokensController)

I follow those conventions automatically, but I have to admit I don’t see any real advantages in the distinction between singular and plural names at the moment. On the other hand, the disadvantages are they only work if you use English names (else you have to define your own rules in app/config/inflections.php), they make the learning curve steeper, and if you look at tables as objects it is not that logical to use plural names for them…

Please don’t get me wrong, I think it is a good thing to have naming conventions, but I also think the current conventions could be simplified, to something like:

  • Table names are singular (e.g. post, category, request_token)
  • Model names are like table names, but camel-cased (e.g. Post, Category, RequestToken)
  • Controller names use the model names plus “Controller” (e.g. PostController, CategoryController, RequestTokenController)

You would get all the benefits of naming conventions without having to care whether a name has to be singular or plural. And the conventions would also work for people who prefer to use names in their native languages.

I know, those singular/plural conventions probably won’t disappear, as they are core conventions and a change would affect many Cake applications… At least I learned something about the design of conventions from this example ;-)

31 comments baked

  • nate

    …and this occurred to you just now?

  • Andreas

    One (little) advantage of having table names pluralized is:
    “group” is a reserved word (in mysql) “groups” is not.
    “limit” is a reserved word, “limits” not.
    “order” is a reserved word, “orders” not…

    Also:
    In a table there are many objects – so it’s plural. A Model represents one Object -> singular. ( that’s not perfectly true for cakePHP now…but perhaps in cakePHP 2.0?)

  • Aeron

    I like the table name as plural. Code is very abstract, so logically table as a collection of records makes sense as plural. I do the same for array variable names. What I don’t understand is why the “controller” appended to every class which is in the “controllers” folder – that to me is redundant. But I also don’t like how ACL uses “lft” and “rght” column names – why not just “lt” and “rt”?

  • Aeron

    As a followup, all controllers are an extension of app_controller, but all models are an extension of app_model yet we are not required to append “model” to the end of every model class….

  • Jonathan Snook

    I’d prefer it if model names were ExampleModel and simply aliased to Example when attached to $this. It’d help prevent name collisions. How often have people tried to create a File model?

  • Michał Szajbe

    I read somewhere that CakePHP was inspired by Ruby on Rails framework, so I think it just “borrowed” this convention from there.

    I don’t know if it’s true, because after working with Rails for some time I believe that this naming convention is the only thing both frameworks have in common :)

  • Khaled

    tables names are plural … this is a convention for a lot of sql developer

  • Jonah

    Personally I like table names to be plural. Which of the following is more readable?

    SELECT * FROM `posts` ORDER BY `date` DESC LIMIT 10

    or

    SELECT * FROM `post` ORDER BY `date` DESC LIMIT 10

  • Phally

    I agree with Andreas. The table names should be plural, the model names should be too, since representing one or more objects.

    I don’t think every object should have a model. It would be a huge performance hit if 300.000 records all had a model and it has no advantages. What would you do with it.

    $this->Users->546123->username = 'Phally';

    Doesn’t make much sense to me.

  • JMG

    Having plural and singular really helps to understand the overall logic. You implicitly makes the difference between $posts and $post. This is just the same. In fact, to be strictly logic, models could be separated in two classes, a plural and a singular one. For instance ‘Posts’ for finding rows and ‘Post’ to handle a single record. That way you would have something like :

    $Post = new Post($id);
    $posts = Posts::findAll();

    Where $posts is an array of Post objects (and Posts makes use of PHP5’s LSB).

  • Aeron

    @JMT

    I think Symfony works something like that – with, for example, Post for the active record (CRUD) and PostPeer for queries and result sets. The problem – at least with the Symfony implementation – is you double the number of models, things get a lot harder to keep track of, at least in your head ;)

  • Grant Cox

    Nate – some insight from a core developer would be useful, your “frist!!” post is just lame.

    With regards to the singular / plural, if it was just consistent between table / model / controller, then the issues with inflector language would be solved, and you could decide per object / project what convention to follow. I’ll second the “Model” suffix too, to avoid namespace conflicts.

  • David Persson

    I filed a ticket regarding the Model suffix a few months ago: https://trac.cakephp.org/ticket/5272
    The main reason for closing given was:
    “[…] Neither helpers, nor shells, nor components, nor behaviors represent the objects of your application’s domain model, which are first-class entities in the application. “

  • Martin Westin

    LIke most people using CakePHP, I am used to the naming conventions.

    Since I was briefly into using Rails before finding Cake (in early 2006) I agree that the naming does make more sense when you create a model object that is supposed to represent a single record.

    If any change should be made I’d say that in Cake it would make just as much sense to call models Posts, Articles since you work with them a lot in the context of a collection of records. But I do not seriously think that any change should occur, except possibly the suffix idea. I am one of those that had an application in 1.1 with a model called File… :)

  • Nik

    Agreed with @Jonah posts are more sensible in SQL statement.

    Also, a framework is like a human language, you have to learn it and use it – obey to the conventions.

  • NOSLOW

    What I like about using conventions is that I don’t have to waste any time thinking (arguing in my head) about stuff like whether something should be singular or plural. Sure you could argue all day long about why is should be this way vs. that way. Pick one, move on and don’t second guess it. Just go with it. Or else you’re wasting time that conventions saves you in the first place.

  • cakebaker

    @all: Thanks for your comments!

    @nate: Yes. There are usually some triggers which force me to look at something. In this case the triggers were the books and my experiments with MySQL Workbench.

    @Andreas: Yes, those words are keywords and shouldn’t be used as table names (even though it is possible to use them).

    I think it depends on your point of view whether a table name should be singular or plural. If you see a table as a container for “objects”, then it probably makes sense to use plural names.

    On the other hand, on a more abstract level, isn’t there a table just a different representation of a model? If you model a “concept” (e.g. a user) in a ER diagram it looks almost the same as if you model it in a UML diagram. In both cases it represents the concept “user”, which defines, for example, that a user has a username and a password. And as there is only one user concept, it makes sense to use singular names.

    @Aeron: See my answer to Andreas.

    I’m not sure whether “Controller” should be removed, personally I appended it to controller classes before I used cake when there was no such convention. In the early cake days there was some discussion about removing it, but in the end it was decided to keep it (see ticket 121).

    I agree that the ACL column names are a bit ugly, but I think “lt” and “rt” are not much better ;-)

    @Jonathan: Hm, I think appending “Model” to model classes to avoid naming collisions is the wrong approach. IMO it would be better to add a “Cake” prefix to the core classes.

    @Michal: Yes, those naming conventions have been borrowed from Rails. Cake uses the same philosophies (convention over configuration) as Rails, but else it is going its own way.

    @Khaled: Well, you find both conventions in use.

    @Jonah: Yes, it is a little bit more readable if you use plural names. On the other hand, cake adds a singular alias to it when executing such statements, so you have SELECT * FROM posts AS Post vs. SELECT * FROM post AS Post, in which case I think the second statement is more readable ;-)

    @Phally: I disagree, see my answer to Andreas.

    @JMG: Yes, from a logical point of view it would make sense to have two classes: one representing the table, and one representing a single record. But I don’t know if it would be practical…

    @Martin: At least to me, a model represents a “concept”, e.g. an article, and not a collection. And so it should have a singular name, see also my answer to Andreas.

    @Nik: Sure, it makes your life easier if you follow the conventions, on the other hand conventions are not fix and can change over time.

    @NOSLOW: Yes, that’s the advantage of naming conventions and what I like about them. But I also think it makes sense to question the conventions from time to time, as conventions are not fix.

  • rafaelbandeira3

    I don’t like the idea of having “Cake” prepended to core classes as it would turn things much more verborragic and ugly. I don’t like namespaced names and I think the take a lot of readability of the code

    $this->File->append($text);
    // is much more intuitive than
    $this->CakeFile->append($text);

    And it’s certainly the kind of stuff that takes me away of frameworks like Symfony and ZF, it makes things look “unatural”… dealing with core classes of cake you feel at home, with auto explanatory names that gives lots of fluidability to the code you’re writing.
    Now, having “Model” appended to model’s name is the missing convention, after all, all other cake’s mvc components have their types appended to their name.

  • joe siao

    Hi,

    I don’t know if using this type of convention would help me making my code more readable too… or is it even relevant…?

  • cakebaker

    @Rafael, Joe: Thanks for your comments!

    @Rafael: Personally I don’t like the prefixing, too. But if you want to avoid naming conflicts you either need such a prefix in the framework or a convention for the framework users (at least as long there is no support for namespaces). The advantage of the prefix is it affects only a few people (the framework developers), whereas in the other case it affects all framework users.

    I don’t think we should have a convention of appending “Model” to model names. Models represent concepts of the problem domain, and so it doesn’t make much sense to have names like UserModel, because you will find a User in the problem domain, not a UserModel… On the other hand it is ok to append the type to the names of the other “components” because those “components” represent abstract software concepts you won’t find in a problem domain.

    Hope that’s not too abstract ;-)

    @Joe: No, it wouldn’t make your code more readable, a class name like “UserController” is as readable as the current “UsersController” in my opinion.

  • Mark Story

    Singular table names don’t really sit well with me, they are storage areas for multiple objects, so plural form makes sense. As for model names staying singular I agree with this as well. For the most part it acts as a single object, and often represents a single row. There are many model methods that work on multiple rows, but that doesn’t diminish the fact that their most common use (at least for me) is to work on a single row at a time. It would be painful in the long run to require a convention like $this->Post->all->find($conditions);. Controllers represent ways to access all objects of a similar type, so again plural makes sense. At the end of the day its very much a tomato/tomatoe discussion. There are plenty of arguments on both sides, and cake has just chosen one of those sides.

  • cakebaker

    @Mark: Thanks for your comment!

    Yes, that’s true, there are two different camps regarding this topic. The article Plural vs Singular Table Names lists the advantages of both approaches.

  • rafaelbandeira3

    @cakebaker: no, you actually putted it very well – as usually – and I had changed my mind about the prefixing stuff… ;-)

  • bob

    A non “plural vs singular” question…

    If you have NamesController.php, why not NameModel.php? Or just Name(s).php in the controllers/models folder?

    Why do we use camel case only some of the time?

  • cakebaker

    @bob: Thanks for your questions!

    From my answer to Rafael: “I don’t think we should have a convention of appending “Model” to model names. Models represent concepts of the problem domain, and so it doesn’t make much sense to have names like UserModel, because you will find a User in the problem domain, not a UserModel…”

    On the other hand I think it makes sense to append “Controller” to controllers, as it describes the role of those classes.

    But I know, cake is not that consistent with the naming conventions at the file level. The UsersController class is in a file users_controller.php whereas the class FormHelper is in the file form.php (and not in form_helper.php). Personally I would prefer to have a convention which says: the filename is like the class name, but underscored.

    I’m not sure what you mean with your last question. Camel case names are used everywhere with the exception of table and file names. And there I think it makes sense to use underscored names, as it is a common practice.

    Hope that answers your questions!

  • Hernando

    I work for a non-english company that has his own conventions for the DB table names, and I find really difficult to work with CakePHP fighting with the naming conventions. You should not tie up programmers to your own conventions. I would really apreciate if someone could tell me a way to create my own conventions maybe?

  • cakebaker

    @Hernando: No, there is no way to tell CakePHP to use your own conventions… CakePHP is an opinionated framework and so it expects you to follow its naming conventions. You can use names not following those conventions, but it won’t make your life easier ;-)

    If your conventions use singular/plural but just in another language, you could try to define some inflection rules in app/config/bootstrap.php, though I don’t know how well this works.

  • Aeron

    This is a silly comment. Cake is a conventions-over-configuration framework. You would use it specifically for it’s conventions.

    Also RTFM:
    Use your own name:
    http://book.cakephp.org/view/1057/Model-Attributes
    Define your own conventions:
    http://book.cakephp.org/view/953/Inflections

    If you want to configure everything yourself Cake is no more work than any other framework. If you want to use Cake’s conventions it is potentially a lot less work.

  • cakebaker

    @Aeron: Thanks for your comment and for providing links to the documentation!

  • Shariff

    Well, it may sound nice, plural table names, but what if the names are not just suffixed with an s. Like City turns into Cities and then you get City in the singular.

  • cakebaker

    @Shariff: CakePHP uses the class Inflector for this purpose, which also supports irregular singular/plural rules. Plus you can define your own inflection rules, if necessary, as described here.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License