Catalog / CakePHP Cheat Sheet
CakePHP Cheat Sheet
A comprehensive cheat sheet for CakePHP, covering conventions, core classes, ORM, and common tasks.
CakePHP Conventions
Naming Conventions
Controllers: |
Plural, PascalCase, ends with |
Models: |
Singular, PascalCase (e.g., |
Tables: |
Plural, PascalCase, ends with |
Views: |
Lowercase, underscored (e.g., |
Database Tables: |
Plural, underscored (e.g., |
Fields in Database: |
Lowercase, underscored (e.g., |
Directory Structure
|
Core Classes & Usage
Controller
|
Loads a model into the controller. |
|
Accesses POST data. |
|
Accesses GET parameters. |
|
Redirects to another action. |
|
Sets variables to be available in the view. |
|
Displays a success flash message. |
Table
|
Retrieves all records. |
|
Retrieves a single record by ID. |
|
Creates a new entity from data. |
|
Patches an entity with new data. |
|
Saves an entity to the database. |
|
Deletes an entity from the database. |
Entity
Entities represent individual table rows. You can access properties directly (e.g., |
ORM Features
Associations
|
Defines a belongs-to association. |
|
Defines a has-many association. |
|
Defines a belongs-to-many association. |
|
Defines a has-one association. |
Eager loading associations |
|
Retrieving Data
|
Find all published articles. |
|
Retrieves data as a key-value list. |
|
Retrieves data as a threaded list. |
|
Retrieves first element. |
Query Builder
CakePHP provides a powerful query builder for constructing complex database queries. Use the |
Common Tasks
Generating URLs
|
Generates a URL to the |
|
Generates a URL using the Router class (outside of views). |
|
Generate path to asset. |
Form Helper
|
Creates a form for the |
|
Creates an input field for the |
|
Creates a submit button. |
|
Closes the form. |
Baking
CakePHP’s baking tool can generate code for models, views, and controllers. Use the |