CakePHP is a powerful PHP framework that follows convention over configuration, particularly in how it names and maps tables, models, and entities. In English-based applications, this usually means:
But what happens when your database tables use Bahasa Melayu (BM) naming conventions where words like pengguna, pelajar, and guru are used instead of users, students, or teachers?
This tutorial shows how to work seamlessly with Malay table names in CakePHP, even when those names don’t follow English pluralization rules.
The Problem: No Plural/Singular Forms in BM
In Bahasa Melayu:
However, CakePHP expects plural table names and singular entity names based on English inflection rules. Without manual configuration, CakePHP might get confused, for example, it might wrongly guess 'Penggunum' as a singular form of pengguna.
Solution 1: Explicit Mapping in Table Class
Let’s say your table is called pengguna.
1. Create the PenggunaTable.php Class
// src/Model/Table/PenggunaTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
class PenggunaTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
// Explicit mapping to the correct table and entity
$this->setTable('pengguna'); // database table name
$this->setEntityClass('App\Model\Entity\Pengguna'); // corresponding entity
}
}
2. Create the Pengguna.php Entity
// src/Model/Entity/Pengguna.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Pengguna extends Entity
{
protected array $_accessible = [
'*' => true,
'id' => false,
];
}
This setup ensures CakePHP doesn't try to "guess" the singular/plural and instead follows what you explicitly define.
Solution 2: Define Custom Inflector Rules (Optional)
If you want CakePHP to understand that pengguna is both singular and plural, you can define custom rules in your config/bootstrap.php:
use Cake\Utility\Inflector;
Inflector::rules('plural', ['irregular' => ['pengguna' => 'pengguna']]);
Inflector::rules('singular', ['irregular' => ['pengguna' => 'pengguna']]);
With this, you can skip setTable() and setEntityClass() in PenggunaTable.php, and CakePHP will still map everything correctly.
Example Usage:
// In your controller
$pengguna = $this->Pengguna->newEmptyEntity();
$pengguna->nama = 'Ali Bin Ahmad';
$this->Pengguna->save($pengguna);
CakePHP now understands:
- You are saving into the pengguna table
- Each record is an instance of the Pengguna entity
Issue: Bake Command with Malay Words
When using CakePHP's Bake tool, it tries to automatically pluralize/singularize names using English rules.
If you run:
bin/cake bake model Pengguna
CakePHP will:
- Assume the table name is penggunas, which doesn't exist.
- Throw an error like:
Exception: Table penggunas for model Pengguna was not found in datasource default.
Solution: Explicitly Set the Table Name
You can still use bake, but you must manually set the table name after baking.
1. Run the bake command
bin/cake bake model Pengguna
bin/cake bake controller Pengguna
bin/cake bake template Pengguna
2. Update the generated PenggunaTable.php
Open src/Model/Table/PenggunaTable.php, and add:
$this->setTable('pengguna'); // Ensure it matches your actual table name
Example:
namespace App\Model\Table;
use Cake\ORM\Table;
class PenggunaTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('pengguna');
$this->setDisplayField('nama');
$this->setPrimaryKey('id');
}
}
| Task | What to do |
| Table in DB | Use real Malay word, e.g., pengguna |
| Bake Command | Use the singular form: Pengguna |
| Fix in PenggunaTable.php | Add $this->setTable('pengguna') manually |
To avoid confusion when using Malay nouns or other non-English terms, it’s good practice to always specify the table name inside your Table class. This ensures CakePHP doesn’t rely on inaccurate pluralization.
Real-World BM Table Names
Here are some common BM table names and their expected setup:
| Table (DB) | Entity Class | Table Class |
| pengguna | Pengguna | PenggunaTable |
| pelajar | Pelajar | PelajarTable |
| guru | Guru | GuruTable |
| pekerja | Pekerja | PekerjaTable |
| pentadbir | Pentadbir | PentadbirTable |
Tip for Multi-language Apps
If your app uses a mix of English and BM, it's best to standardize your naming strategy. Either:
- Stick to BM consistently for table/entity names, or
- Use English for code-level naming, and BM for labels/translations
CakePHP’s i18n system can help translate UI labels into BM, while keeping backend logic clean.
By following these steps, you can develop CakePHP applications using Bahasa Melayu terms, while keeping full compatibility with Cake’s conventions.
Thats all, happy coding :)