-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,16 +236,6 @@ public function trashAll($conditions): int | |
); | ||
} | ||
|
||
/** | ||
* Deletes all rows marked as `trashed`. | ||
* | ||
* @return int | ||
*/ | ||
public function emptyTrash(): int | ||
{ | ||
return $this->_table->deleteAll($this->_getUnaryExpression()); | ||
} | ||
|
||
/** | ||
* Restores all (or given) trashed row(s). | ||
* | ||
|
@@ -306,6 +296,57 @@ public function cascadingRestoreTrash(?EntityInterface $entity = null, array $op | |
return $result; | ||
} | ||
|
||
/** | ||
* Deletes all (or given) rows marked as `trashed`. | ||
* | ||
* @param \Cake\Datasource\EntityInterface|null $entity to delete. | ||
* @return bool|\Cake\Datasource\EntityInterface|int | ||
*/ | ||
public function emptyTrash(?EntityInterface $entity = null) | ||
{ | ||
if ($entity instanceof EntityInterface) { | ||
return $this->_table->deleteAll(['id' => $entity->id]) ? $entity : false; | ||
} | ||
|
||
return $this->_table->deleteAll($this->_getUnaryExpression()); | ||
} | ||
|
||
/** | ||
* Delete an item from trashed status and all its related data | ||
* | ||
* @param \Cake\Datasource\EntityInterface $entity Entity instance | ||
* @return bool|\Cake\Datasource\EntityInterface|int | ||
*/ | ||
public function cascadingEmptyTrash(?EntityInterface $entity = null) | ||
{ | ||
$result = $this->emptyTrash($entity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
/** @var \Cake\ORM\Association $association */ | ||
foreach ($this->_table->associations() as $association) { | ||
if ($this->_isRecursable($association, $this->_table)) { | ||
if ($entity === null) { | ||
$result += $association->getTarget()->cascadingEmptyTrash(null); | ||
} else { | ||
$foreignKey = (array)$association->getForeignKey(); | ||
$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 commentThe 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 |
||
if ( | ||
!$association | ||
->getTarget() | ||
->cascadingEmptyTrash($related) | ||
) { | ||
$result = false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Returns a unary expression for bulk record manipulation. | ||
* | ||
|
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.