Download the PHP package mirko-pagliai/cakephp-entities-logger without Composer

On this page you can find all versions of the php package mirko-pagliai/cakephp-entities-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cakephp-entities-logger

cakephp-entities-logger

CI codecov Codacy Badge CodeFactor

Logs who created, updated or deleted your CakePHP entities, and when.

Add a behavior to the tables you want, and the plugin writes a log every time an entity of those tables is created, updated or deleted. Each log stores:

For example, after a few changes the entities_logs table contains:

id entity_class entity_id user_id type datetime ip user_agent
1 App\Model\Entity\Article 1 1 created 2026-10-20 09:30:00 192.168.0.10 Mozilla/5.0 (…) Firefox/131.0
2 App\Model\Entity\Article 2 1 created 2026-10-20 09:42:00 192.168.0.10 Mozilla/5.0 (…) Firefox/131.0
3 App\Model\Entity\Article 1 2 updated 2026-10-20 15:10:00 2001:db8:85a3::8a2e:370:7334 Mozilla/5.0 (…) Chrome/130.0
4 App\Model\Entity\Article 2 1 deleted 2026-10-21 08:05:00 192.168.0.10 Mozilla/5.0 (…) Firefox/131.0

The ip column holds both IPv4 and IPv6 addresses. The user agents are shortened here.

The plugin tells you who changed an entity and when, but it does not store what has changed: the values of the fields are not saved.

Requirements

Installation

You can install the plugin via composer:

Then you have to load the plugin. For more information on how to load the plugin, please refer to the CakePHP documentation.

Simply, you can execute the shell command to enable the plugin:

This would update your application's bootstrap method.

Create the table

Now you need to create the table that the plugin will use to keep changes to the entities you want.

The best way is using migrations (if your application doesn't have it yet, composer require cakephp/migrations):

Instead, if you want to verify that the plugin migrations have been applied correctly:

Add the behavior

Add the Cake/EntitiesLogger.EntitiesLog behavior to the tables you want.

Inside the initialize() method of your tables:

Reading the logs

The behavior adds an EntitiesLogs association to the table, so the logs of an entity can be read like any other association, from the oldest to the newest:

Containing Users is optional: it also loads the user of each log ($EntitiesLog->user).

A deleted entity no longer exists, so it cannot be loaded this way. To read its logs, query EntitiesLogsTable directly, by entity_class and entity_id.

What is logged

Each log is a row of the entities_logs table:

Column Description
entity_class The entity class (with its namespace) of the table that loaded the behavior
entity_id The primary key of the entity (an integer)
user_id The user who made the change
type created, updated or deleted
datetime When the change was made
ip The IP address of the request (IPv4 or IPv6)
user_agent The user agent of the request

The entity_class and entity_id columns have an index, used to find the logs of an entity.

The logs are written by the afterSave() and afterDelete() callbacks, so only when entities are saved or deleted through their table (save(), saveMany(), delete(), and so on). For this reason:

Some things to keep in mind:

Options

The behavior accepts the following options:

Merging close updates

A user who changes the same entity several times in a short time (for example, it is saved, a mistake is noticed and it is saved again) would leave a log for each change. For this reason, when an entity is updated, a new log is not added if the last log of that entity:

In this case the datetime of that log is updated, so the log always shows the time of the last change. Nothing else is written or deleted.

Some things to keep in mind:

Grouping entities

Some entities make sense only as part of a group. For example, the lines of an order are always handled together, and what you want to log is that the order has been changed, no matter which line.

With the groupField option you set the name of the field that identifies the group (here, order_id):

With this option:

If the table already has logs, don't add this option: they identify the entities by their primary key, so they would be mixed up with those of the groups.

Requests and identity

The log stores the user who made the change, so it is created only when there is a request:

Supported primary keys

The entity_id column of the entities_logs table is an integer. For this reason, the tables that load the behavior must have a single-column primary key (it doesn't have to be named id) whose value is an integer.

If the primary key is composite or its value is not an integer (for example, a UUID), the log cannot be saved and an UnexpectedValueException is thrown when the entity is saved or deleted.

The same applies to the identity stored in the request (the user), whose id must be an integer.

Entity class

The logs of an entity are identified by the entity_class and entity_id columns, and the EntitiesLogs association added by the behavior finds the logs whose entity_class is exactly the entity class of the table. For this reason, the logs of entities of other classes (even subclasses) are not found.

The entity class must identify the table: each table that loads the behavior must have its own entity class. Tables that share the same entity class would also share the logs (the same entity_id of two tables would be mixed up), so the generic Cake\ORM\Entity class, which CakePHP uses when a table has no entity class of its own, is not accepted: an UnexpectedValueException is thrown when the entity is saved or deleted.

IP address

The IP address is taken from ServerRequest::clientIp(). If the application is behind a proxy or a load balancer, the request must trust it (ServerRequest::$trustProxy), otherwise the address of the proxy is logged instead of the one of the client.

Both IPv4 and IPv6 addresses are supported. The ip column holds up to 39 characters, the length of an IPv6 address in its full form, and the address is stored as ServerRequest::clientIp() returns it, without normalizing it.

The address must be a valid IP address, without a port or a zone id (such as 203.0.113.5:4444 or fe80::1%eth0). Otherwise the log does not pass the validation and a PersistenceFailedException is thrown, so the entity is not saved either. This can happen if a trusted proxy adds the port to the X-Forwarded-For header.

Logs on a different connection

The log is saved in the afterSave() and afterDelete() callbacks of the table that loaded the behavior, so inside the transaction of the entity being saved or deleted.

If you store the logs on a different connection (for example, with your own EntitiesLogsTable that overrides defaultConnectionName()), the two cannot be rolled back together: if the transaction is rolled back after the log has been written (for example, when the save is part of a larger transaction), the log is kept.

In this case, the plugin migrations must be run on that connection too (here logs), otherwise they would try to modify the entities_logs table of the default connection, which doesn't exist:

The afterSaveCommit() and afterDeleteCommit() callbacks are not used, because CakePHP does not dispatch those events when the save is part of a larger transaction, and the log would be lost.

Customizing

To use your own table for the logs (for example, to add a finder, or to store the logs on a different connection), extend Cake\EntitiesLogger\Model\Table\EntitiesLogsTable, and extend the behavior so that it uses your table:

Then add your own behavior to the tables, instead of the one of the plugin:

The table must be set before calling the parent constructor, because the constructor uses it as the target of the EntitiesLogs association. The protected methods buildEntity() and saveEntitiesLog() can be overridden too.

Your table must be named EntitiesLogsTable, so that its alias is EntitiesLogs, the one used by the association to sort the logs. It also needs its own entity class, that extends the one of the plugin. By default CakePHP looks for it next to your table, so for App\Model\Table\EntitiesLogsTable it is App\Model\Entity\EntitiesLog:

If that class does not exist, CakePHP would use the generic Cake\ORM\Entity and saving the logs would fail. If your entity has another name or namespace, set it in the initialize() method of your table with setEntityClass().

Versioning

For transparency and insight into our release cycle and to maintain backward compatibility, this package will be maintained under the Semantic Versioning guidelines.


All versions of cakephp-entities-logger with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
cakephp/cakephp Version ^5.1.10
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mirko-pagliai/cakephp-entities-logger contains the following files

Loading the files please wait ...