Menu

Tuesday, 9 June 2026

Create CRUD using Cake PHP

 

CakePHP has a powerful code generation tool called ​Bake​. Since you already have your database schema ready in your ERD, Bake can read your database tables and automatically generate the entire CRUD infrastructure (Models, Views, Controllers, and Test suites) in seconds.

Here is the step-by-step process to generate CRUD pages for your ERD using Laragon.

Before running Bake, your CakePHP application needs to connect to a database that matches your ERD schema.

  1. Open HeidiSQL or phpMyAdmin via Laragon.
  2. Create a new database (e.g., cake_app).
  3. Execute your SQL DDL script (the SQL generated from your ERD) to build these tables in the database.
  4. Open your CakePHP project folder, navigate to config/app_local.php, and update your database credentials:

Step 2: Open Laragon Terminal

  1. Open the Laragon interface and click ​Terminal​.
  2. Navigate to your project folder:

Step 3: Bake the CRUD Pages

You can bake everything table-by-table, or use a master command to generate everything at once.

Option A: Bake everything all at once (Fastest)

Run the following command to scan your database and build the complete CRUD structure for every single table:

Bash

bin/cake bake all --all

CakePHP will automatically detect foreign key relationships (like trainerId in sessions or customerId in enrollment) and build the correct object associations (belongsTo, hasMany).

If you want to ensure everything hooks up nicely step-by-step, bake the tables individually using the all command followed by the table name (use camelCase or lowercase matching your tables):

Bash

bin/cake bake all trainers
bin/cake bake all traineraddresses
bin/cake bake all trainercontacts
bin/cake bake all skills
bin/cake bake all admin
bin/cake bake all courses
bin/cake bake all sessions
bin/cake bake all customers
bin/cake bake all customeraddresses
bin/cake bake all customercontacts
bin/cake bake all enrollment

Step 4: Verify the Generated Files

Once Bake finishes running, it creates files across three main folders in your src/ directory:

  • ​**src/Model/**​: Contains Entity and Table files mapping out your validation rules and relationships (e.g., SessionsTable.php will show it belongsTo both Courses and Trainers).
  • ​**src/Controller/**​: Contains controllers with standard CRUD actions (index, view, add, edit, delete).
  • ​**templates/**​: Contains the visual frontend .php files for your pages (e.g., templates/Customers/index.php).

Step 5: Test Your New CRUD Pages

Make sure Laragon is running, then open your browser to access your tables directly via their controller names:

  • Trainers CRUD: http://my_cake_app.test/trainers
  • Customers CRUD: http://my_cake_app.test/customers
  • Sessions CRUD: http://my_cake_app.test/sessions

When you click "New Session" or ​**"New Enrollment"**​, CakePHP will automatically turn your foreign key columns into clean dropdown selection menus populated by the related data!


No comments:

Post a Comment