Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Naming conventions

A convention is a naming rule. CakePHP uses the controller name to find a table and a template, and a column name to find a link between tables. While names follow the rules, nothing has to be configured. This is the first thing a beginner should learn.

One complete example

Take blog articles. Everything related to them is named like this:

WhatRuleName
Database tablePlural, lower casearticles
Table classTable name plus TableArticlesTable
Entity classSingularArticle
ControllerPlural plus ControllerArticlesController
Template folderController name without Controllertemplates/Articles/
AddressController name in lower case/articles

Where the files are

src/Controller/ArticlesController.php
src/Model/Table/ArticlesTable.php
src/Model/Entity/Article.php
templates/Articles/index.php
templates/Articles/view.php

A file name always matches the class name. A template is named after the controller method: index.php for index(), view.php for view().

Names of several words

In the database words are separated by underscores, in classes every word starts with a capital letter, in addresses words are joined by hyphens:

WhatName
Tablearticle_comments
Table classArticleCommentsTable
Entity classArticleComment
ControllerArticleCommentsController
Address/article-comments
Template for the viewAll() methodtemplates/ArticleComments/view_all.php

Table columns

ColumnMeaning
idPrimary key, the record number
createdCreation date, filled in automatically
modifiedModification date, filled in automatically
user_idA reference to a record of the users table

A foreign key is a column that stores the number of a record in another table. It is named after that table in the singular with the _id ending. A table that joins two others is named after both in alphabetical order: articles_tags.

How it looks in code

// src/Controller/ArticlesController.php
namespace App\Controller;
 
class ArticlesController extends AppController
{
    public function index(): void
    {
        $articles = $this->Articles->find()->all();
 
        $this->set('articles', $articles);
    }
}

There is no table name and no template path here. From the name ArticlesController the framework knows that $this->Articles is the articles table and that templates/Articles/index.php must be rendered.

When a name cannot be changed

An existing database with a blog_posts table is connected with a setting in the table class:

// src/Model/Table/ArticlesTable.php
public function initialize(array $config): void
{
    $this->setTable('blog_posts');
}

Conventions can be bypassed, but every exception costs an extra line of code.

Common mistake

A table called article instead of articles, or a controller called ArticleController, and CakePHP reports that the table does not exist or the template is missing. The error looks mysterious although the cause is a single letter. Before searching the code, compare your names with the tables in this lesson.

Thanks for reading!

Donate to the site — we work hard for you.