What is the difference between table and entity in CakePHP?

Prior to CakePHP version 3, a single class named "model" used to interact with the database for data flow. After the release of version 3, the "model" class was separated into table and entity classes to follow the SRP (Single Responsibility Principle) principle.

Code example

Let's have a look at the following example, where we have to interact with a database table called Movies.

MoviesTable.php
Movie.php
<?php
// File: src/Model/Entity/Movie.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Movie extends Entity
{
// properties of an individual row in the movies table.
}

To access the Movies table, we have created a corresponding table class, MoviesTable, in the MoviesTable.php file. This class extends from App\ORM\Table indicating that it represents the Movies table present in the database. We have created an entity object, Movie, to represent an individual movie or a row in the table.

Some core differences between table and entity are given below:

Representation

In CakePHP, tables represent database tables, while entity objects are individual rows.

Purpose

A table class serves as a gateway for interacting with database records and storing them. On the other hand, entity objects provide data in a more meaningful way. Each entity provides methods for dealing with the data stored in a specific row.

Methods

The table class provides methods for managing relationships with other tables and performing CRUD (Create, Read, Update, Delete) operations on the database. A few methods are shown below in accordance with the Movies table discussed above:

<?php
// Method to find movies using specific conditions
$movies = Movies->find(<condition>);
// Method to perform edits on movies
Movies->patchEntity(<entity>,<data_to_be_added>);
// Method to save a new or existing movie present in the movies table
Movies->save($movies);

Entities can fetch, modify, and persist data using the table's methods. Following are a few methods associated with entities:

<?php
// Method to get the value of the id property of a movie
$id = $movie->get('id');
// Method to set the value of a property
$movie->set('id', 4);
// Method to save the entity to the database
$movie->save();
// Method to delete the entity from the database
$movie->delete();

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved