Skip to content
CODE THE PIXEL
CakePHP

Handling Irregular Plural Table Names in CakePHP

Asyraf Wahi Anuar 4 min read

Handling Irregular Plural Table Names in CakePHP

Tech stack

  • CakePHP
  • PHP

CakePHP follows the principle of "convention over configuration", which means it minimizes the amount of code and configuration you need by assuming logical, consistent naming conventions. One key convention is that database table names should be in the plural form. In CakePHP, database table naming follows a convention where plural table names (e.g., users) map to singular entity classes (e.g., User), and corresponding table classes (e.g., UsersTable).

For example:

Element

Name

Table (DB)

Users

Model Class

User

Table Class

UsersTable

Controller

UsersController

Entity Class

User

This convention helps CakePHP automatically determine the relationships between:

  • Tables in the database

  • Their corresponding Table classes (for database logic)

  • Entity classes (representing single records)

By following the plural naming convention (e.g., users, articles, comments), you let CakePHP "just work" without needing to explicitly define table names or override defaults. However, what happens when you encounter irregular plural words like teeth or children? These don’t follow standard pluralization rules and can confuse the framework. Some English words have irregular plurals, such as:

Singular

Plural

Tooth

Teeth

Child

Children

Person

People

This tutorial will walk you through how to properly set up a table like teeth in CakePHP so it maps cleanly to a Tooth entity by following best practices.

Step 1: Create the Table

Create a table in your database called teeth:

HTML
CREATE TABLE teeth (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    created DATETIME,
    modified DATETIME
);

Step 2: Create the Table and Entity Classes

TeethTable.php
Create the TeethTable class under src/Model/Table/.

HTML
// src/Model/Table/TeethTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class TeethTable extends Table
{
    public function initialize(array $config): void
    {
        parent::initialize($config);

        // Explicitly set the table and entity class
        $this->setTable('teeth');
        $this->setEntityClass('App\Model\Entity\Tooth');
    }
}

Tooth.php
Create the Tooth entity class under src/Model/Entity/.

HTML
// src/Model/Entity/Tooth.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Tooth extends Entity
{
    protected array $_accessible = [
        '*' => true,
        'id' => false,
    ];
}

Optional: Define Inflector Rules

CakePHP uses Inflector to guess plural/singular forms. By default, it doesn't know "tooth" ↔ "teeth". You can define it manually in config/bootstrap.php:

bootstrap.phpHTML
use Cake\Utility\Inflector;

Inflector::rules('plural', ['irregular' => ['tooth' => 'teeth']]);
Inflector::rules('singular', ['irregular' => ['teeth' => 'tooth']]);

This allows you to skip setTable() and setEntityClass() in TeethTable.php because CakePHP will now guess correctly.

Usage Example

In your controller or elsewhere, you can now do:

HTML
// In a controller
$tooth = $this->Teeth->newEmptyEntity();
$tooth->name = 'Upper Right Molar';
$this->Teeth->save($tooth);

CakePHP will know that:

  • You’re inserting into the teeth table

  • Each row is represented by a Tooth entity

Element

Name

Table (DB)

teeth

Table Class

TeethTable

Entity Class

Tooth

Optional Inflection

tooth ↔ teeth

This pattern keeps your domain language meaningful and your code clean, no weird names like teeths required.

While you can name your table teeths to “trick” CakePHP into guessing correctly, it's not semantically correct and may cause confusion later. Use Inflector::rules() or manual mapping for the best results.

That all, happy coding :)

Did this tutorial help?

0 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read

User Interface

Dark Mode in Bootstrap 5.3.3

Dark Mode is a popular feature with a darker colour scheme, making it easier on the eyes, especially in low-light environments. It enhances the visual appeal and improves user experience by reducing eye strain and saving battery life on OLED screens.

2 min read