How to delegate "delete" operation to another Model in Cakephp4?

  cakephp-4.x, model, php

I have 2 Models (SuperRubriques and CustomRubriques) using the same table rubriques in DB.

When I delete from SuperRubriques, I would like to deleguate the delete to CustomRubriques (as CustomRubriques has a hasOne association with extended_rubriques that SuperRubriques doesn’t know).

For info, the rubriques table in DB has the field model containing 'CustomRubriques' (i.e the Model with witch it has been saved).

I’ve tried to do it in SuperRubriquesTable::beforeDete() :

// In SuperRubriquesTable.php
public function beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)
{
    $table = TableRegistry::getTableLocator()->get($entity->model); // $entity->model contains 'CustomRubriques'
    $rubriqueEntity = $table->get($entity->id);
    return $table->delete($rubriqueEntity);
}

However $table->delete($rubriqueEntity) is true (when I debug) but the record is not deleted in DB, I don’t why ?

Source: Ask PHP

LEAVE A COMMENT