diff --git a/src/ORM/Association/DependentDeleteHelper.php b/src/ORM/Association/DependentDeleteHelper.php new file mode 100644 index 00000000000..11f69c3924a --- /dev/null +++ b/src/ORM/Association/DependentDeleteHelper.php @@ -0,0 +1,59 @@ +getDependent()) { + return true; + } + $table = $association->getTarget(); + $foreignKey = (array)$association->getForeignKey(); + $bindingKey = (array)$association->getBindingKey(); + $conditions = array_combine($foreignKey, $entity->extract($bindingKey)); + + if ($association->getCascadeCallbacks()) { + foreach ($association->find()->where($conditions)->toList() as $related) { + $table->delete($related, $options); + } + + return true; + } + $conditions = array_merge($conditions, $association->getConditions()); + + return (bool)$table->deleteAll($conditions); + } +} diff --git a/src/ORM/Association/DependentDeleteTrait.php b/src/ORM/Association/DependentDeleteTrait.php index b3614f2eaa6..c36c64f73b5 100644 --- a/src/ORM/Association/DependentDeleteTrait.php +++ b/src/ORM/Association/DependentDeleteTrait.php @@ -15,11 +15,14 @@ namespace Cake\ORM\Association; use Cake\Datasource\EntityInterface; +use Cake\ORM\Association\DependentDeleteHelper; /** * Implements cascading deletes for dependent associations. * * Included by HasOne and HasMany association classes. + * + * @deprected 3.5.0 Unused in CakePHP now. This class will be removed in 4.0.0 */ trait DependentDeleteTrait { @@ -35,24 +38,7 @@ trait DependentDeleteTrait */ public function cascadeDelete(EntityInterface $entity, array $options = []) { - if (!$this->getDependent()) { - return true; - } - $table = $this->getTarget(); - $foreignKey = (array)$this->getForeignKey(); - $bindingKey = (array)$this->getBindingKey(); - $conditions = array_combine($foreignKey, $entity->extract($bindingKey)); - - if ($this->_cascadeCallbacks) { - foreach ($this->find()->where($conditions)->toList() as $related) { - $table->delete($related, $options); - } - - return true; - } - - $conditions = array_merge($conditions, $this->getConditions()); - - return $table->deleteAll($conditions); + $helper = new DependentDeleteHelper(); + return $helper->cascadeDelete($this, $entity, $options); } } diff --git a/src/ORM/Association/HasMany.php b/src/ORM/Association/HasMany.php index 8ef85db240b..cd3d47891e7 100644 --- a/src/ORM/Association/HasMany.php +++ b/src/ORM/Association/HasMany.php @@ -20,6 +20,7 @@ use Cake\Database\Expression\QueryExpression; use Cake\Datasource\EntityInterface; use Cake\ORM\Association; +use Cake\ORM\Association\DependentDeleteHelper; use Cake\ORM\Association\Loader\SelectLoader; use Cake\ORM\Table; use InvalidArgumentException; @@ -34,8 +35,6 @@ class HasMany extends Association { - use DependentDeleteTrait; - /** * Order in which target records should be returned * @@ -655,4 +654,13 @@ public function eagerLoader(array $options) return $loader->buildEagerLoader($options); } + + /** + * {@inheritDoc} + */ + public function cascadeDelete(EntityInterface $entity, array $options = []) + { + $helper = new DependentDeleteHelper(); + return $helper->cascadeDelete($this, $entity, $options); + } } diff --git a/src/ORM/Association/HasOne.php b/src/ORM/Association/HasOne.php index 0be3521dded..b2ea68ab38d 100644 --- a/src/ORM/Association/HasOne.php +++ b/src/ORM/Association/HasOne.php @@ -16,6 +16,7 @@ use Cake\Datasource\EntityInterface; use Cake\ORM\Association; +use Cake\ORM\Association\DependentDeleteHelper; use Cake\ORM\Association\Loader\SelectLoader; use Cake\ORM\Table; use Cake\Utility\Inflector; @@ -28,9 +29,6 @@ */ class HasOne extends Association { - - use DependentDeleteTrait; - /** * Valid strategies for this type of association * @@ -145,4 +143,13 @@ public function eagerLoader(array $options) return $loader->buildEagerLoader($options); } + + /** + * {@inheritDoc} + */ + public function cascadeDelete(EntityInterface $entity, array $options = []) + { + $helper = new DependentDeleteHelper(); + return $helper->cascadeDelete($this, $entity, $options); + } }