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?
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?
Thank you very much for your reply.