There is no AppModel
base class anymore as of CakePHP 3.
Events
If you want to run "after save" logic for all models, then you can now use events, specifically the Model.afterSave
and Model.afterSaveCommit
events.
So for afterSave
you can register an event like this:
\Cake\Event\EventManager::instance()->on(
'Model.afterSave',
function (
\Cake\Event\EventInterface $event,
\Cake\Datasource\EntityInterface $entity,
\ArrayObject $options
) {
// run your action logging logic here
}
);
To cover the whole application you'd usually register such an event in your application's bootstrap, specifically in the Application::bootstrap()
method in the src/Application.php
file, not in the config/bootstrap.php
file (this matters very much for the test environment!).
See also
Behaviors
If you wanted to apply this to only specific models, then a behavior would be the tool of choice. You'd really only need to define the callback just like you would in your table class:
// in src/Model/Behavior/ActionLogBehavior.php
namespace App\Model\Behavior;
use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
class ActionLogBehavior extends Behavior
{
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
// run your action logging logic here
}
}
Then you can add the behavior to the respective table classes via their initialize()
method, like:
public function initialize(array $config): void
{
// ...
$this->addBehavior('ActionLog');
}
See also