Override the delete function in the model is it possible or any other way do it?

i want override the delete function in the model some additional feature in delete function So how can do it this and how to use the beforeDelete event my model any help.

You want to override the delete function, or you want to use the beforeDelete event, or both?

To override the function, you just make your own delete function with the same parameters as the Table class you’re extending. Do make sure to call the parent version. But what are you trying to do in that function that you can’t already do with the beforeDelete event?

To use the beforeDelete event, see the Events System for general instructions and beforeDelete for specifics.

well which one is works well that useful for me. By the way i trying to override but i can’t do that properly i think because it throw error.

in Model define function like

public function delete($id=null){
// delete code
}

same prototype defined in my controller then why it is throwing error ? Is that wrong defining ?

The delete function of your model will overload (the technical term, rather than “override”) the function from the base model class. Your definition needs to match that one exactly. It takes an entity rather than an int, for starters.

Well i doing wrong flow. I implemented an UsersDeleteBehavior . When i call delete method, I need to delete the record as well as that entity stored at another table .
How to do that in behavior Please any sort of code will help me or such type functionality to do in my project with any other way.

You have an association between two tables, and when you delete a record from one you need the related record(s) to be deleted from the other? That’s just a cascading delete, easily handled by a trivial bit of configuration, no separate behavior required.

If it’s something more complicated than that, then more specifics may be required in order to answer.

You don’t understand my query. will give you e.g.
it is define in UsersFindBehavior

public function findForUser(\Cake\ORM\Query $query, array $options)
{
return $query->where([‘user_id’ => $options[‘user_id’]]);
}

same like above i want do before deleting any record my custom code will run that it .

You’re only giving tiny windows into what you’re trying to do, but not why or in what context. It sounds a bit like an XY problem. Can you explain the high-level problem you’re trying to solve instead?

:blush: I need to implement Record delete and restore functionality in my project. but i don’t want any plugins just like soft-delete. So I am implemented behavior in my project. If any user by mistake delete the records then Administrator will restore the deleted record. It is just like maintain logs. but i want that stored into database table instead of file.

That’s much clearer now! Not sure why this or this isn’t suitable for you, but you can presumably get implementation cues from them? Or from this post?

1 Like

Thank you very much for your reply. :blush: :relaxed: