CakePHP conventions

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

3 Comments

  1. pz
    Posted July 7, 2006 at 7:35 am | Permalink

    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.

  2. danny h
    Posted July 27, 2006 at 7:50 pm | Permalink

    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…

  3. Posted July 28, 2006 at 8:14 am | Permalink

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


Post a Comment

Required fields are marked *
*
*

%d bloggers like this: