Skip to content

Commit 815e98a

Browse files
authored
Update QueryExpression comparison names for CakePHP 6 (#8388)
1 parent a045505 commit 815e98a

2 files changed

Lines changed: 54 additions & 31 deletions

File tree

‎docs/en/appendices/6-0-migration-guide.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ Some properties have also been renamed to better reflect their purpose. These ar
138138

139139
### Database
140140

141+
- The comparison methods on `Cake\Database\Expression\QueryExpression` have
142+
been renamed to match the predicate functions. The old names have been removed:
143+
144+
| Previous method | New method |
145+
| --- | --- |
146+
| `eq()` | `equals()` |
147+
| `notEq()` | `notEquals()` |
148+
| `gt()` | `greaterThan()` |
149+
| `gte()` | `greaterThanOrEquals()` |
150+
| `lt()` | `lessThan()` |
151+
| `lte()` | `lessThanOrEquals()` |
152+
153+
Update method calls and overrides in subclasses. Arguments, binding types,
154+
and fluent chaining keep their existing behavior. The
155+
[upgrade tool](6-0-upgrade-guide#upgrade-tool-use) includes these renames in its
156+
`cakephp60` rules. See
157+
[Building Conditions with Expressions](../orm/query-builder#building-conditions-with-expressions)
158+
for updated examples.
159+
141160
- Operators in condition-array keys, such as `['views >' => 1000]` or
142161
`['id IN' => $ids]`, are no longer supported and raise an
143162
`InvalidArgumentException`. Use the predicate functions in

‎docs/en/orm/query-builder.md‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ anonymous function will receive an instance of
277277
```php
278278
$query = $articles->find();
279279
$query->where(function (QueryExpression $exp, SelectQuery $q) {
280-
return $exp->eq('published', true);
280+
return $exp->equals('published', true);
281281
});
282282
```
283283

@@ -722,9 +722,9 @@ $query = $cities->find()
722722
->where(function (QueryExpression $exp, SelectQuery $q) {
723723
return $exp->addCase(
724724
[
725-
$q->expr()->lt('population', 100000),
725+
$q->expr()->lessThan('population', 100000),
726726
$q->expr()->between('population', 100000, 999000),
727-
$q->expr()->gte('population', 999001),
727+
$q->expr()->greaterThanOrEquals('population', 999001),
728728
],
729729
['SMALL', 'MEDIUM', 'LARGE'], # values matching conditions
730730
['string', 'string', 'string'], # type of each value
@@ -745,7 +745,7 @@ $query = $cities->find()
745745
->where(function (QueryExpression $exp, SelectQuery $q) {
746746
return $exp->addCase(
747747
[
748-
$q->expr()->eq('population', 0),
748+
$q->expr()->equals('population', 0),
749749
],
750750
['DESERTED', 'INHABITED'], # values matching conditions
751751
['string', 'string'], # type of each value
@@ -1246,10 +1246,10 @@ The `QueryExpression` passed to the callback function defaults to `and()`:
12461246
$query = $articles->find()
12471247
->where(function (QueryExpression $exp) {
12481248
return $exp
1249-
->eq('author_id', 2)
1250-
->eq('published', true)
1251-
->notEq('spam', true)
1252-
->gt('view_count', 10);
1249+
->equals('author_id', 2)
1250+
->equals('published', true)
1251+
->notEquals('spam', true)
1252+
->greaterThan('view_count', 10);
12531253
});
12541254
```
12551255

@@ -1274,12 +1274,12 @@ following:
12741274
$query = $articles->find()
12751275
->where(function (QueryExpression $exp) {
12761276
$orConditions = $exp->or(['author_id' => 2])
1277-
->eq('author_id', 5);
1277+
->equals('author_id', 5);
12781278

12791279
return $exp
12801280
->add($orConditions)
1281-
->eq('published', true)
1282-
->gte('view_count', 10);
1281+
->equals('published', true)
1282+
->greaterThanOrEquals('view_count', 10);
12831283
});
12841284
```
12851285

@@ -1303,13 +1303,13 @@ the method chaining:
13031303
$query = $articles->find()
13041304
->where(function (QueryExpression $exp) {
13051305
$orConditions = $exp->or(function (QueryExpression $or) {
1306-
return $or->eq('author_id', 2)
1307-
->eq('author_id', 5);
1306+
return $or->equals('author_id', 2)
1307+
->equals('author_id', 5);
13081308
});
13091309

13101310
return $exp
13111311
->not($orConditions)
1312-
->lte('view_count', 10);
1312+
->lessThanOrEquals('view_count', 10);
13131313
});
13141314
```
13151315

@@ -1319,11 +1319,11 @@ You can negate sub-expressions using `not()`:
13191319
$query = $articles->find()
13201320
->where(function (QueryExpression $exp) {
13211321
$orConditions = $exp->or(['author_id' => 2])
1322-
->eq('author_id', 5);
1322+
->equals('author_id', 5);
13231323

13241324
return $exp
13251325
->not($orConditions)
1326-
->lte('view_count', 10);
1326+
->lessThanOrEquals('view_count', 10);
13271327
});
13281328
```
13291329

@@ -1348,8 +1348,8 @@ $query = $articles->find()
13481348
]);
13491349

13501350
return $exp
1351-
->gte($year, 2014)
1352-
->eq('published', true);
1351+
->greaterThanOrEquals($year, 2014)
1352+
->equals('published', true);
13531353
});
13541354
```
13551355

@@ -1364,25 +1364,29 @@ WHERE (
13641364
)
13651365
```
13661366

1367+
The comparison methods on `QueryExpression` use the same names as the
1368+
[predicate functions](#predicate-functions). Pass the field as the first
1369+
argument, followed by the value and an optional binding type.
1370+
13671371
When using the expression objects you can use the following methods to create
13681372
conditions:
13691373

1370-
- `eq()` Creates an equality condition:
1374+
- `equals()` Creates an equality condition:
13711375

13721376
```php
13731377
$query = $cities->find()
13741378
->where(function (QueryExpression $exp, SelectQuery $q) {
1375-
return $exp->eq('population', '10000');
1379+
return $exp->equals('population', '10000');
13761380
});
13771381
# WHERE population = 10000
13781382
```
13791383

1380-
- `notEq()` Creates an inequality condition:
1384+
- `notEquals()` Creates an inequality condition:
13811385

13821386
```php
13831387
$query = $cities->find()
13841388
->where(function (QueryExpression $exp, SelectQuery $q) {
1385-
return $exp->notEq('population', '10000');
1389+
return $exp->notEquals('population', '10000');
13861390
});
13871391
# WHERE population != 10000
13881392
```
@@ -1450,42 +1454,42 @@ conditions:
14501454
# WHERE (country_id NOT IN ('AFG', 'USA', 'EST') OR country_id IS NULL)
14511455
```
14521456

1453-
- `gt()` Create a `>` condition:
1457+
- `greaterThan()` Create a `>` condition:
14541458

14551459
```php
14561460
$query = $cities->find()
14571461
->where(function (QueryExpression $exp, SelectQuery $q) {
1458-
return $exp->gt('population', '10000');
1462+
return $exp->greaterThan('population', '10000');
14591463
});
14601464
# WHERE population > 10000
14611465
```
14621466

1463-
- `gte()` Create a `>=` condition:
1467+
- `greaterThanOrEquals()` Create a `>=` condition:
14641468

14651469
```php
14661470
$query = $cities->find()
14671471
->where(function (QueryExpression $exp, SelectQuery $q) {
1468-
return $exp->gte('population', '10000');
1472+
return $exp->greaterThanOrEquals('population', '10000');
14691473
});
14701474
# WHERE population >= 10000
14711475
```
14721476

1473-
- `lt()` Create a `<` condition:
1477+
- `lessThan()` Create a `<` condition:
14741478

14751479
```php
14761480
$query = $cities->find()
14771481
->where(function (QueryExpression $exp, SelectQuery $q) {
1478-
return $exp->lt('population', '10000');
1482+
return $exp->lessThan('population', '10000');
14791483
});
14801484
# WHERE population < 10000
14811485
```
14821486

1483-
- `lte()` Create a `<=` condition:
1487+
- `lessThanOrEquals()` Create a `<=` condition:
14841488

14851489
```php
14861490
$query = $cities->find()
14871491
->where(function (QueryExpression $exp, SelectQuery $q) {
1488-
return $exp->lte('population', '10000');
1492+
return $exp->lessThanOrEquals('population', '10000');
14891493
});
14901494
# WHERE population <= 10000
14911495
```
@@ -1647,7 +1651,7 @@ $query->select([
16471651
'year' => $query->func()->year([$query->identifier('created')]),
16481652
])
16491653
->where(function ($exp, $query) {
1650-
return $exp->gt('population', 100000);
1654+
return $exp->greaterThan('population', 100000);
16511655
});
16521656
```
16531657

0 commit comments

Comments
 (0)