UseMuffin / Trash

Adds soft-delete support to CakePHP ORM.
MIT License
85 stars 35 forks source link

cascadeDeleting not working with emptyTrash() #56

Open frandss opened 2 years ago

frandss commented 2 years ago

I'm using CakePHP 3.10.4, Compras (purchases) -> hasMany -> Carritos (cart items) -> hasMany -> Entradas (show tickets)

If any purchase is deleted "soft delete with delete()", purchase, cart items and tickets update deleted field correctly. Code: $entity = $this->Compras->findById($id)->find('translations')->first(); $this->Compras->delete($entity);

Later, if a deleted purchase is deleted "hard delete with emptyTrash()", only purchase is removed from database. Code: $entity = $this->Compras->find('onlyTrashed')->find('translations')->where(['Compras.id' => $id])->first(); $this->Compras->emptyTrash($entity);

Additional data: ComprasTable: [...] $this->hasMany('Ventas.Carritos') ->setForeignKey('compra_id') ->setDependent(true) ->setCascadeCallbacks(true) ->setSort(['Carritos.producto_id' => 'asc', 'Carritos.modelo_id' => 'asc', 'Carritos.experiencia_id' => 'asc', 'Carritos.cantidad' => 'asc']); [...]

CarritosTable: [...] $this->belongsTo('Ventas.Compras') ->setFinder('withTrashed'); $this->hasMany('Ticketing.Entradas') ->setForeignKey('carrito_id') ->setDependent(true) ->setCascadeCallbacks(true); [...]

EntradasTable: [...] $this->belongsTo('Ventas.Carritos') ->setFinder('withTrashed'); [...]

ADmad commented 2 years ago

emptyTrash() uses Table::deleteAll() which does not use callbacks. You can submit a pull request if you like adding a $cascade argument to the method and updating internals accordingly.