-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cascadingEmptyTrash method… #57
base: master
Are you sure you want to change the base?
Conversation
…to new changes, preserving backward compatibility. Add related info to readme.
$bindingKey = (array)$association->getBindingKey(); | ||
$conditions = array_combine($foreignKey, $entity->extract($bindingKey)); | ||
|
||
foreach ($association->find('onlyTrashed')->where($conditions) as $related) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a record is being hard deleted then all it's dependent records should be hard deleted, not just the ones which are trashed, otherwise you could end up with orphaned records. So the withTrashed
finder should be used here not onlyTrashed
.
*/ | ||
public function cascadingEmptyTrash(?EntityInterface $entity = null) | ||
{ | ||
$result = $this->emptyTrash($entity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dependent records should be deleted before parents, otherwise you will get delete failures if foreign key constraint is set on the dependent table.
* @param \Cake\Datasource\EntityInterface|null $entity to delete. | ||
* @return bool|\Cake\Datasource\EntityInterface|int | ||
*/ | ||
public function emptyTrash(?EntityInterface $entity = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't mix deletion of single and multiple records, it makes the API ugly. Static analyzers won't be able to detect the proper return type.
Besides deleting of associated records the use of So a better way to solve all problems would be to just use |
Just ran into this same issue today. Need to delete files only when a record has been hard-deleted. An option to use callbacks would be great or an event like |
If I understand correctly the topic, I set It would be nice to be able to set cascadeCallbacks to true by default for all relations (to avoid accidentally forgetting) so any and all related deletions trigger delete events. |
…and modify emptyTrash method to adapt to new changes, preserving backward compatibility. Add related info to readme.