Skip to content

Commit bbdaab7

Browse files
authored
Fix detection of unaliased identifiers (#72)
Added test for implicit conditions and fix detection of unaliased identifiers in the query
1 parent baaf4d5 commit bbdaab7

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/Model/Behavior/TrashBehavior.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use ArrayObject;
77
use Cake\Core\Configure;
88
use Cake\Core\Exception\CakeException;
9-
use Cake\Database\Expression\BetweenExpression;
10-
use Cake\Database\Expression\ComparisonExpression;
9+
use Cake\Database\Expression\FieldInterface;
1110
use Cake\Database\Expression\IdentifierExpression;
1211
use Cake\Database\Query\SelectQuery;
1312
use Cake\Datasource\EntityInterface;
@@ -181,41 +180,39 @@ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObjec
181180
return;
182181
}
183182

184-
$field = $this->getTrashField();
185-
186-
if ($this->shouldAddTrashCondition($query, $field)) {
187-
$query->andWhere([$field . ' IS' => null]);
183+
if ($this->shouldAddTrashCondition($query)) {
184+
$query->andWhere([$this->getTrashField() . ' IS' => null]);
188185
}
189186
}
190187

191188
/**
192189
* Whether we need to add the trash condition to the query
193190
*
194191
* @param \Cake\ORM\Query\SelectQuery $query Query.
195-
* @param string $field Trash field
196192
* @return bool
197193
*/
198-
protected function shouldAddTrashCondition(SelectQuery $query, string $field): bool
194+
protected function shouldAddTrashCondition(SelectQuery $query): bool
199195
{
196+
$fieldIdentifiers = [$this->getTrashField(false), $this->getTrashField()];
200197
$addCondition = true;
201198

202-
$query->traverseExpressions(function ($expression) use (&$addCondition, $field): void {
199+
$query->traverseExpressions(function ($expression) use (&$addCondition, $fieldIdentifiers): void {
203200
if (!$addCondition) {
204201
return;
205202
}
206203

207204
if (
208205
$expression instanceof IdentifierExpression
209-
&& $expression->getIdentifier() === $field
206+
&& in_array($expression->getIdentifier(), $fieldIdentifiers, true)
210207
) {
211208
$addCondition = false;
212209

213210
return;
214211
}
215212

216213
if (
217-
($expression instanceof ComparisonExpression || $expression instanceof BetweenExpression)
218-
&& $expression->getField() === $field
214+
$expression instanceof FieldInterface
215+
&& in_array($expression->getField(), $fieldIdentifiers, true)
219216
) {
220217
$addCondition = false;
221218
}

tests/TestCase/Model/Behavior/TrashBehaviorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ public function testTrashNonAccessibleProperty()
361361
$this->assertCount(3, $this->Articles->find('withTrashed'));
362362
}
363363

364+
public function testFindWithImplicitCondition()
365+
{
366+
$this->assertCount(2, $this->Articles->find()->where([
367+
'trashed IS NOT' => null,
368+
]));
369+
370+
$this->assertCount(2, $this->Articles->find()->where([
371+
'Articles.trashed IS NOT' => null,
372+
]));
373+
}
374+
364375
/**
365376
* Test it can find only trashed records.
366377
*

0 commit comments

Comments
 (0)