CakePHP conventions

Published on and tagged with cakephp  general

CakePHP uses the convention over configuration approach. gwoo has added the conventions used by CakePHP to the startpage of the wiki. As they are essential, I will repeat them here:

  • table names are plural and lowercased
  • model names are singular and CamelCased: ModelName
  • model filenames are singular and underscored: model_name.php
  • controller names are plural and CamelCased with *Controller* appended: ControllerNamesController
  • controller filenames are plural and underscored with *controller* appended: controller_names_controller.php
  • associations should use the ModelName, and the order should match the order of the foreignKeys: var $belongsTo = ‘User’;
  • foreign keys should always be: table_name_in_singular_form_id: user_id (foreign key) → users (table)
  • many-to-many join tables should be named: alphabetically_first_table_plural_alphabetically_second_table_plural: tags_users
  • columns in many-to-many join tables should be named like other foreign keys: tag_id and user_id
  • columns named “created” and “modified” will automatically be populated correctly

7 comments baked

  • pz

    what’s the naming convention for join tables? in the blog example that’s in the manual, post has many tags and vice versa, so there is a posts_tags table. Would the model name for posts_tags table be PostTag??? I don’t think it works.

  • danny h

    I have had a few probs with this as well, there are conflicting instructions in various places – but no definitive answer.

    I followed the manual and Cake kicked off about the model/controller names.

    I know for sure we all need to work together to improve the documentation ( and tutorials ) for Cake at the wiki.

    The cake developers must be pretty busy….

    Its not that I have problems with PHP, its just that when you are trying to stick to the cake conventions finding solutions can be time consuming, and to some extent make us all newbies again…

  • cakebaker

    @danny h: What was the problem? Use the conventions from http://manual.cakephp.org/chapter/22

  • Les conventions de CakePHP - Pierre MARTIN

    […] quelques liens qui m’ont servi de base et qui pourront vous être utile. Cela se passe sur Cakebaker, CakePourTous (les bosses du chameau, vous vous rappelez ?) et bien sur dans le manuel. Si vous […]

  • Anonymous

    Yea, now I’m confused. I read that cake can create the join tables for you but I’ve never gotten it to do that. Perhaps it’s best just to manually do these things.

  • cakebaker

    @Anonymous: No, Cake doesn’t automatically create database tables for you.

  • Ranjeet

    now my some confusion is gone from your article
    Thanks a lot

Bake a comment




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

© daniel hofstetter. Licensed under a Creative Commons License