diff --git a/src/Model/Behavior/TrashBehavior.php b/src/Model/Behavior/TrashBehavior.php index 6a58019..609983a 100644 --- a/src/Model/Behavior/TrashBehavior.php +++ b/src/Model/Behavior/TrashBehavior.php @@ -180,7 +180,26 @@ public function trash(EntityInterface $entity, array $options = []): bool */ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void { + if (!empty($options['skipAddTrashCondition'])) { + return; + } + $field = $this->getTrashField(); + + if ($this->shouldAddTrashCondition($query, $field)) { + $query->andWhere([$field . ' IS' => null]); + } + } + + /** + * Whether we need to add the trash condition to the query + * + * @param \Cake\ORM\Query\SelectQuery $query Query. + * @param string $field Trash field + * @return bool + */ + protected function shouldAddTrashCondition(SelectQuery $query, string $field): bool + { $addCondition = true; $query->traverseExpressions(function ($expression) use (&$addCondition, $field): void { @@ -205,11 +224,7 @@ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObjec } }); - $option = $query->getOptions(); - - if ($addCondition && empty($option['skipAddTrashCondition'])) { - $query->andWhere($query->newExpr()->isNull($field)); - } + return $addCondition; } /** @@ -221,7 +236,9 @@ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObjec */ public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery { - return $query->andWhere($query->newExpr()->isNotNull($this->getTrashField())); + return $query + ->applyOptions(['skipAddTrashCondition' => true]) + ->andWhere($query->newExpr()->isNotNull($this->getTrashField())); } /**