-
Notifications
You must be signed in to change notification settings - Fork 18
/
Query.php
1442 lines (1324 loc) · 47 KB
/
Query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;
use ArrayObject;
use BadMethodCallException;
use Cake\Database\Connection;
use Cake\Database\ExpressionInterface;
use Cake\Database\Query as DatabaseQuery;
use Cake\Database\TypedResultInterface;
use Cake\Database\TypeMap;
use Cake\Database\ValueBinder;
use Cake\Datasource\QueryInterface;
use Cake\Datasource\QueryTrait;
use Cake\Datasource\ResultSetInterface;
use InvalidArgumentException;
use JsonSerializable;
use RuntimeException;
use Traversable;
/**
* Extends the base Query class to provide new methods related to association
* loading, automatic fields selection, automatic type casting and to wrap results
* into a specific iterator that will be responsible for hydrating results if
* required.
*
* @see \Cake\Collection\CollectionInterface For a full description of the collection methods supported by this class
* @property \Cake\ORM\Table $_repository Instance of a table object this query is bound to.
* @method \Cake\ORM\Table getRepository() Returns the default table object that will be used by this query,
* that is, the table that will appear in the from clause.
* @method \Cake\Collection\CollectionInterface each(callable $c) Passes each of the query results to the callable
* @method \Cake\Collection\CollectionInterface sortBy($callback, int $dir) Sorts the query with the callback
* @method \Cake\Collection\CollectionInterface filter(callable $c = null) Keeps the results using passing the callable test
* @method \Cake\Collection\CollectionInterface reject(callable $c) Removes the results passing the callable test
* @method bool every(callable $c) Returns true if all the results pass the callable test
* @method bool some(callable $c) Returns true if at least one of the results pass the callable test
* @method \Cake\Collection\CollectionInterface map(callable $c) Modifies each of the results using the callable
* @method mixed reduce(callable $c, $zero = null) Folds all the results into a single value using the callable.
* @method \Cake\Collection\CollectionInterface extract($field) Extracts a single column from each row
* @method mixed max($field) Returns the maximum value for a single column in all the results.
* @method mixed min($field) Returns the minimum value for a single column in all the results.
* @method \Cake\Collection\CollectionInterface groupBy(string|callable $field) In-memory group all results by the value of a column.
* @method \Cake\Collection\CollectionInterface indexBy(string|callable $callback) Returns the results indexed by the value of a column.
* @method \Cake\Collection\CollectionInterface countBy(string|callable $field) Returns the number of unique values for a column
* @method float sumOf(string|callable $field) Returns the sum of all values for a single column
* @method \Cake\Collection\CollectionInterface shuffle() In-memory randomize the order the results are returned
* @method \Cake\Collection\CollectionInterface sample(int $size = 10) In-memory shuffle the results and return a subset of them.
* @method \Cake\Collection\CollectionInterface take(int $size = 1, int $from = 0) In-memory limit and offset for the query results.
* @method \Cake\Collection\CollectionInterface skip(int $howMany) Skips some rows from the start of the query result.
* @method mixed last() Return the last row of the query result
* @method \Cake\Collection\CollectionInterface append(array|\Traversable $items) Appends more rows to the result of the query.
* @method \Cake\Collection\CollectionInterface combine($k, $v, $g = null) Returns the values of the column $v index by column $k,
* and grouped by $g.
* @method \Cake\Collection\CollectionInterface nest($k, $p, $n = 'children') Creates a tree structure by nesting the values of column $p into that
* with the same value for $k using $n as the nesting key.
* @method array toArray() Returns a key-value array with the results of this query.
* @method array toList() Returns a numerically indexed array with the results of this query.
* @method \Cake\Collection\CollectionInterface stopWhen(callable $c) Returns each row until the callable returns true.
* @method \Cake\Collection\CollectionInterface zip(array|\Traversable $c) Returns the first result of both the query and $c in an array,
* then the second results and so on.
* @method \Cake\Collection\CollectionInterface zipWith($collections, callable $callable) Returns each of the results out of calling $c
* with the first rows of the query and each of the items, then the second rows and so on.
* @method \Cake\Collection\CollectionInterface chunk(int $size) Groups the results in arrays of $size rows each.
* @method bool isEmpty() Returns true if this query found no results.
*/
class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
{
use QueryTrait {
cache as private _cache;
all as private _all;
_decorateResults as private _applyDecorators;
__call as private _call;
}
/**
* Indicates that the operation should append to the list
*
* @var int
*/
public const APPEND = 0;
/**
* Indicates that the operation should prepend to the list
*
* @var int
*/
public const PREPEND = 1;
/**
* Indicates that the operation should overwrite the list
*
* @var bool
*/
public const OVERWRITE = true;
/**
* Whether the user select any fields before being executed, this is used
* to determined if any fields should be automatically be selected.
*
* @var bool|null
*/
protected $_hasFields;
/**
* Tracks whether or not the original query should include
* fields from the top level table.
*
* @var bool|null
*/
protected $_autoFields;
/**
* Whether to hydrate results into entity objects
*
* @var bool
*/
protected $_hydrate = true;
/**
* Whether aliases are generated for fields.
*
* @var bool
*/
protected $aliasingEnabled = true;
/**
* A callable function that can be used to calculate the total amount of
* records this query will match when not using `limit`
*
* @var callable|null
*/
protected $_counter;
/**
* Instance of a class responsible for storing association containments and
* for eager loading them when this query is executed
*
* @var \Cake\ORM\EagerLoader|null
*/
protected $_eagerLoader;
/**
* True if the beforeFind event has already been triggered for this query
*
* @var bool
*/
protected $_beforeFindFired = false;
/**
* The COUNT(*) for the query.
*
* When set, count query execution will be bypassed.
*
* @var int|null
*/
protected $_resultsCount;
/**
* Constructor
*
* @param \Cake\Database\Connection $connection The connection object
* @param \Cake\ORM\Table $table The table this query is starting on
*/
public function __construct(Connection $connection, Table $table)
{
parent::__construct($connection);
$this->repository($table);
if ($this->_repository !== null) {
$this->addDefaultTypes($this->_repository);
}
}
/**
* Adds new fields to be returned by a `SELECT` statement when this query is
* executed. Fields can be passed as an array of strings, array of expression
* objects, a single expression or a single string.
*
* If an array is passed, keys will be used to alias fields using the value as the
* real field to be aliased. It is possible to alias strings, Expression objects or
* even other Query objects.
*
* If a callable function is passed, the returning array of the function will
* be used as the list of fields.
*
* By default this function will append any passed argument to the list of fields
* to be selected, unless the second argument is set to true.
*
* ### Examples:
*
* ```
* $query->select(['id', 'title']); // Produces SELECT id, title
* $query->select(['author' => 'author_id']); // Appends author: SELECT id, title, author_id as author
* $query->select('id', true); // Resets the list: SELECT id
* $query->select(['total' => $countQuery]); // SELECT id, (SELECT ...) AS total
* $query->select(function ($query) {
* return ['article_id', 'total' => $query->count('*')];
* })
* ```
*
* By default no fields are selected, if you have an instance of `Cake\ORM\Query` and try to append
* fields you should also call `Cake\ORM\Query::enableAutoFields()` to select the default fields
* from the table.
*
* If you pass an instance of a `Cake\ORM\Table` or `Cake\ORM\Association` class,
* all the fields in the schema of the table or the association will be added to
* the select clause.
*
* @param array|\Cake\Database\ExpressionInterface|callable|string|\Cake\ORM\Table|\Cake\ORM\Association $fields Fields
* to be added to the list.
* @param bool $overwrite whether to reset fields with passed list or not
* @return $this
*/
public function select($fields = [], bool $overwrite = false)
{
if ($fields instanceof Association) {
$fields = $fields->getTarget();
}
if ($fields instanceof Table) {
if ($this->aliasingEnabled) {
$fields = $this->aliasFields($fields->getSchema()->columns(), $fields->getAlias());
} else {
$fields = $fields->getSchema()->columns();
}
}
return parent::select($fields, $overwrite);
}
/**
* All the fields associated with the passed table except the excluded
* fields will be added to the select clause of the query. Passed excluded fields should not be aliased.
* After the first call to this method, a second call cannot be used to remove fields that have already
* been added to the query by the first. If you need to change the list after the first call,
* pass overwrite boolean true which will reset the select clause removing all previous additions.
*
* @param \Cake\ORM\Table|\Cake\ORM\Association $table The table to use to get an array of columns
* @param string[] $excludedFields The un-aliased column names you do not want selected from $table
* @param bool $overwrite Whether to reset/remove previous selected fields
* @return $this
* @throws \InvalidArgumentException If Association|Table is not passed in first argument
*/
public function selectAllExcept($table, array $excludedFields, bool $overwrite = false)
{
if ($table instanceof Association) {
$table = $table->getTarget();
}
if (!($table instanceof Table)) {
throw new InvalidArgumentException('You must provide either an Association or a Table object');
}
$fields = array_diff($table->getSchema()->columns(), $excludedFields);
if ($this->aliasingEnabled) {
$fields = $this->aliasFields($fields);
}
return $this->select($fields, $overwrite);
}
/**
* Hints this object to associate the correct types when casting conditions
* for the database. This is done by extracting the field types from the schema
* associated to the passed table object. This prevents the user from repeating
* themselves when specifying conditions.
*
* This method returns the same query object for chaining.
*
* @param \Cake\ORM\Table $table The table to pull types from
* @return $this
*/
public function addDefaultTypes(Table $table)
{
$alias = $table->getAlias();
$map = $table->getSchema()->typeMap();
$fields = [];
foreach ($map as $f => $type) {
$fields[$f] = $fields[$alias . '.' . $f] = $fields[$alias . '__' . $f] = $type;
}
$this->getTypeMap()->addDefaults($fields);
return $this;
}
/**
* Sets the instance of the eager loader class to use for loading associations
* and storing containments.
*
* @param \Cake\ORM\EagerLoader $instance The eager loader to use.
* @return $this
*/
public function setEagerLoader(EagerLoader $instance)
{
$this->_eagerLoader = $instance;
return $this;
}
/**
* Returns the currently configured instance.
*
* @return \Cake\ORM\EagerLoader
*/
public function getEagerLoader(): EagerLoader
{
if ($this->_eagerLoader === null) {
$this->_eagerLoader = new EagerLoader();
}
return $this->_eagerLoader;
}
/**
* Sets the list of associations that should be eagerly loaded along with this
* query. The list of associated tables passed must have been previously set as
* associations using the Table API.
*
* ### Example:
*
* ```
* // Bring articles' author information
* $query->contain('Author');
*
* // Also bring the category and tags associated to each article
* $query->contain(['Category', 'Tag']);
* ```
*
* Associations can be arbitrarily nested using dot notation or nested arrays,
* this allows this object to calculate joins or any additional queries that
* must be executed to bring the required associated data.
*
* ### Example:
*
* ```
* // Eager load the product info, and for each product load other 2 associations
* $query->contain(['Product' => ['Manufacturer', 'Distributor']);
*
* // Which is equivalent to calling
* $query->contain(['Products.Manufactures', 'Products.Distributors']);
*
* // For an author query, load his region, state and country
* $query->contain('Regions.States.Countries');
* ```
*
* It is possible to control the conditions and fields selected for each of the
* contained associations:
*
* ### Example:
*
* ```
* $query->contain(['Tags' => function ($q) {
* return $q->where(['Tags.is_popular' => true]);
* }]);
*
* $query->contain(['Products.Manufactures' => function ($q) {
* return $q->select(['name'])->where(['Manufactures.active' => true]);
* }]);
* ```
*
* Each association might define special options when eager loaded, the allowed
* options that can be set per association are:
*
* - `foreignKey`: Used to set a different field to match both tables, if set to false
* no join conditions will be generated automatically. `false` can only be used on
* joinable associations and cannot be used with hasMany or belongsToMany associations.
* - `fields`: An array with the fields that should be fetched from the association.
* - `finder`: The finder to use when loading associated records. Either the name of the
* finder as a string, or an array to define options to pass to the finder.
* - `queryBuilder`: Equivalent to passing a callable instead of an options array.
*
* ### Example:
*
* ```
* // Set options for the hasMany articles that will be eagerly loaded for an author
* $query->contain([
* 'Articles' => [
* 'fields' => ['title', 'author_id']
* ]
* ]);
* ```
*
* Finders can be configured to use options.
*
* ```
* // Retrieve translations for the articles, but only those for the `en` and `es` locales
* $query->contain([
* 'Articles' => [
* 'finder' => [
* 'translations' => [
* 'locales' => ['en', 'es']
* ]
* ]
* ]
* ]);
* ```
*
* When containing associations, it is important to include foreign key columns.
* Failing to do so will trigger exceptions.
*
* ```
* // Use a query builder to add conditions to the containment
* $query->contain('Authors', function ($q) {
* return $q->where(...); // add conditions
* });
* // Use special join conditions for multiple containments in the same method call
* $query->contain([
* 'Authors' => [
* 'foreignKey' => false,
* 'queryBuilder' => function ($q) {
* return $q->where(...); // Add full filtering conditions
* }
* ],
* 'Tags' => function ($q) {
* return $q->where(...); // add conditions
* }
* ]);
* ```
*
* If called with an empty first argument and `$override` is set to true, the
* previous list will be emptied.
*
* @param array|string $associations List of table aliases to be queried.
* @param callable|bool $override The query builder for the association, or
* if associations is an array, a bool on whether to override previous list
* with the one passed
* defaults to merging previous list with the new one.
* @return $this
*/
public function contain($associations, $override = false)
{
$loader = $this->getEagerLoader();
if ($override === true) {
$this->clearContain();
}
$queryBuilder = null;
if (is_callable($override)) {
$queryBuilder = $override;
}
if ($associations) {
$loader->contain($associations, $queryBuilder);
}
$this->_addAssociationsToTypeMap(
$this->getRepository(),
$this->getTypeMap(),
$loader->getContain()
);
return $this;
}
/**
* @return array
*/
public function getContain(): array
{
return $this->getEagerLoader()->getContain();
}
/**
* Clears the contained associations from the current query.
*
* @return $this
*/
public function clearContain()
{
$this->getEagerLoader()->clearContain();
$this->_dirty();
return $this;
}
/**
* Used to recursively add contained association column types to
* the query.
*
* @param \Cake\ORM\Table $table The table instance to pluck associations from.
* @param \Cake\Database\TypeMap $typeMap The typemap to check for columns in.
* This typemap is indirectly mutated via Cake\ORM\Query::addDefaultTypes()
* @param array $associations The nested tree of associations to walk.
* @return void
*/
protected function _addAssociationsToTypeMap(Table $table, TypeMap $typeMap, array $associations): void
{
foreach ($associations as $name => $nested) {
if (!$table->hasAssociation($name)) {
continue;
}
$association = $table->getAssociation($name);
$target = $association->getTarget();
$primary = (array)$target->getPrimaryKey();
if (empty($primary) || $typeMap->type($target->aliasField($primary[0])) === null) {
$this->addDefaultTypes($target);
}
if (!empty($nested)) {
$this->_addAssociationsToTypeMap($target, $typeMap, $nested);
}
}
}
/**
* Adds filtering conditions to this query to only bring rows that have a relation
* to another from an associated table, based on conditions in the associated table.
*
* This function will add entries in the `contain` graph.
*
* ### Example:
*
* ```
* // Bring only articles that were tagged with 'cake'
* $query->matching('Tags', function ($q) {
* return $q->where(['name' => 'cake']);
* });
* ```
*
* It is possible to filter by deep associations by using dot notation:
*
* ### Example:
*
* ```
* // Bring only articles that were commented by 'markstory'
* $query->matching('Comments.Users', function ($q) {
* return $q->where(['username' => 'markstory']);
* });
* ```
*
* As this function will create `INNER JOIN`, you might want to consider
* calling `distinct` on this query as you might get duplicate rows if
* your conditions don't filter them already. This might be the case, for example,
* of the same user commenting more than once in the same article.
*
* ### Example:
*
* ```
* // Bring unique articles that were commented by 'markstory'
* $query->distinct(['Articles.id'])
* ->matching('Comments.Users', function ($q) {
* return $q->where(['username' => 'markstory']);
* });
* ```
*
* Please note that the query passed to the closure will only accept calling
* `select`, `where`, `andWhere` and `orWhere` on it. If you wish to
* add more complex clauses you can do it directly in the main query.
*
* @param string $assoc The association to filter by
* @param callable|null $builder a function that will receive a pre-made query object
* that can be used to add custom conditions or selecting some fields
* @return $this
*/
public function matching(string $assoc, ?callable $builder = null)
{
$result = $this->getEagerLoader()->setMatching($assoc, $builder)->getMatching();
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
$this->_dirty();
return $this;
}
/**
* Creates a LEFT JOIN with the passed association table while preserving
* the foreign key matching and the custom conditions that were originally set
* for it.
*
* This function will add entries in the `contain` graph.
*
* ### Example:
*
* ```
* // Get the count of articles per user
* $usersQuery
* ->select(['total_articles' => $query->func()->count('Articles.id')])
* ->leftJoinWith('Articles')
* ->group(['Users.id'])
* ->enableAutoFields();
* ```
*
* You can also customize the conditions passed to the LEFT JOIN:
*
* ```
* // Get the count of articles per user with at least 5 votes
* $usersQuery
* ->select(['total_articles' => $query->func()->count('Articles.id')])
* ->leftJoinWith('Articles', function ($q) {
* return $q->where(['Articles.votes >=' => 5]);
* })
* ->group(['Users.id'])
* ->enableAutoFields();
* ```
*
* This will create the following SQL:
*
* ```
* SELECT COUNT(Articles.id) AS total_articles, Users.*
* FROM users Users
* LEFT JOIN articles Articles ON Articles.user_id = Users.id AND Articles.votes >= 5
* GROUP BY USers.id
* ```
*
* It is possible to left join deep associations by using dot notation
*
* ### Example:
*
* ```
* // Total comments in articles by 'markstory'
* $query
* ->select(['total_comments' => $query->func()->count('Comments.id')])
* ->leftJoinWith('Comments.Users', function ($q) {
* return $q->where(['username' => 'markstory']);
* })
* ->group(['Users.id']);
* ```
*
* Please note that the query passed to the closure will only accept calling
* `select`, `where`, `andWhere` and `orWhere` on it. If you wish to
* add more complex clauses you can do it directly in the main query.
*
* @param string $assoc The association to join with
* @param callable|null $builder a function that will receive a pre-made query object
* that can be used to add custom conditions or selecting some fields
* @return $this
*/
public function leftJoinWith(string $assoc, ?callable $builder = null)
{
$result = $this->getEagerLoader()
->setMatching($assoc, $builder, [
'joinType' => Query::JOIN_TYPE_LEFT,
'fields' => false,
])
->getMatching();
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
$this->_dirty();
return $this;
}
/**
* Creates an INNER JOIN with the passed association table while preserving
* the foreign key matching and the custom conditions that were originally set
* for it.
*
* This function will add entries in the `contain` graph.
*
* ### Example:
*
* ```
* // Bring only articles that were tagged with 'cake'
* $query->innerJoinWith('Tags', function ($q) {
* return $q->where(['name' => 'cake']);
* });
* ```
*
* This will create the following SQL:
*
* ```
* SELECT Articles.*
* FROM articles Articles
* INNER JOIN tags Tags ON Tags.name = 'cake'
* INNER JOIN articles_tags ArticlesTags ON ArticlesTags.tag_id = Tags.id
* AND ArticlesTags.articles_id = Articles.id
* ```
*
* This function works the same as `matching()` with the difference that it
* will select no fields from the association.
*
* @param string $assoc The association to join with
* @param callable|null $builder a function that will receive a pre-made query object
* that can be used to add custom conditions or selecting some fields
* @return $this
* @see \Cake\ORM\Query::matching()
*/
public function innerJoinWith(string $assoc, ?callable $builder = null)
{
$result = $this->getEagerLoader()
->setMatching($assoc, $builder, [
'joinType' => Query::JOIN_TYPE_INNER,
'fields' => false,
])
->getMatching();
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
$this->_dirty();
return $this;
}
/**
* Adds filtering conditions to this query to only bring rows that have no match
* to another from an associated table, based on conditions in the associated table.
*
* This function will add entries in the `contain` graph.
*
* ### Example:
*
* ```
* // Bring only articles that were not tagged with 'cake'
* $query->notMatching('Tags', function ($q) {
* return $q->where(['name' => 'cake']);
* });
* ```
*
* It is possible to filter by deep associations by using dot notation:
*
* ### Example:
*
* ```
* // Bring only articles that weren't commented by 'markstory'
* $query->notMatching('Comments.Users', function ($q) {
* return $q->where(['username' => 'markstory']);
* });
* ```
*
* As this function will create a `LEFT JOIN`, you might want to consider
* calling `distinct` on this query as you might get duplicate rows if
* your conditions don't filter them already. This might be the case, for example,
* of the same article having multiple comments.
*
* ### Example:
*
* ```
* // Bring unique articles that were commented by 'markstory'
* $query->distinct(['Articles.id'])
* ->notMatching('Comments.Users', function ($q) {
* return $q->where(['username' => 'markstory']);
* });
* ```
*
* Please note that the query passed to the closure will only accept calling
* `select`, `where`, `andWhere` and `orWhere` on it. If you wish to
* add more complex clauses you can do it directly in the main query.
*
* @param string $assoc The association to filter by
* @param callable|null $builder a function that will receive a pre-made query object
* that can be used to add custom conditions or selecting some fields
* @return $this
*/
public function notMatching(string $assoc, ?callable $builder = null)
{
$result = $this->getEagerLoader()
->setMatching($assoc, $builder, [
'joinType' => Query::JOIN_TYPE_LEFT,
'fields' => false,
'negateMatch' => true,
])
->getMatching();
$this->_addAssociationsToTypeMap($this->getRepository(), $this->getTypeMap(), $result);
$this->_dirty();
return $this;
}
/**
* Populates or adds parts to current query clauses using an array.
* This is handy for passing all query clauses at once.
*
* The method accepts the following query clause related options:
*
* - fields: Maps to the select method
* - conditions: Maps to the where method
* - limit: Maps to the limit method
* - order: Maps to the order method
* - offset: Maps to the offset method
* - group: Maps to the group method
* - having: Maps to the having method
* - contain: Maps to the contain options for eager loading
* - join: Maps to the join method
* - page: Maps to the page method
*
* All other options will not affect the query, but will be stored
* as custom options that can be read via `getOptions()`. Furthermore
* they are automatically passed to `Model.beforeFind`.
*
* ### Example:
*
* ```
* $query->applyOptions([
* 'fields' => ['id', 'name'],
* 'conditions' => [
* 'created >=' => '2013-01-01'
* ],
* 'limit' => 10,
* ]);
* ```
*
* Is equivalent to:
*
* ```
* $query
* ->select(['id', 'name'])
* ->where(['created >=' => '2013-01-01'])
* ->limit(10)
* ```
*
* Custom options can be read via `getOptions()`:
*
* ```
* $query->applyOptions([
* 'fields' => ['id', 'name'],
* 'custom' => 'value',
* ]);
* ```
*
* Here `$options` will hold `['custom' => 'value']` (the `fields`
* option will be applied to the query instead of being stored, as
* it's a query clause related option):
*
* ```
* $options = $query->getOptions();
* ```
*
* @param array $options The options to be applied
* @return $this
* @see getOptions()
*/
public function applyOptions(array $options)
{
$valid = [
'fields' => 'select',
'conditions' => 'where',
'join' => 'join',
'order' => 'order',
'limit' => 'limit',
'offset' => 'offset',
'group' => 'group',
'having' => 'having',
'contain' => 'contain',
'page' => 'page',
];
ksort($options);
foreach ($options as $option => $values) {
if (isset($valid[$option], $values)) {
$this->{$valid[$option]}($values);
} else {
$this->_options[$option] = $values;
}
}
return $this;
}
/**
* Creates a copy of this current query, triggers beforeFind and resets some state.
*
* The following state will be cleared:
*
* - autoFields
* - limit
* - offset
* - map/reduce functions
* - result formatters
* - order
* - containments
*
* This method creates query clones that are useful when working with subqueries.
*
* @return static
*/
public function cleanCopy()
{
$clone = clone $this;
$clone->triggerBeforeFind();
$clone->disableAutoFields();
$clone->limit(null);
$clone->order([], true);
$clone->offset(null);
$clone->mapReduce(null, null, true);
$clone->formatResults(null, self::OVERWRITE);
$clone->setSelectTypeMap(new TypeMap());
$clone->decorateResults(null, true);
return $clone;
}
/**
* Clears the internal result cache and the internal count value from the current
* query object.
*
* @return $this
*/
public function clearResult()
{
$this->_dirty();
return $this;
}
/**
* {@inheritDoc}
*
* Handles cloning eager loaders.
*/
public function __clone()
{
parent::__clone();
if ($this->_eagerLoader !== null) {
$this->_eagerLoader = clone $this->_eagerLoader;
}
}
/**
* {@inheritDoc}
*
* Returns the COUNT(*) for the query. If the query has not been
* modified, and the count has already been performed the cached
* value is returned
*
* @return int
*/
public function count(): int
{
if ($this->_resultsCount === null) {
$this->_resultsCount = $this->_performCount();
}
return $this->_resultsCount;
}
/**
* Performs and returns the COUNT(*) for the query.
*
* @return int
*/
protected function _performCount(): int
{
$query = $this->cleanCopy();
$counter = $this->_counter;
if ($counter !== null) {
$query->counter(null);
return (int)$counter($query);
}
$complex = (
$query->clause('distinct') ||
count($query->clause('group')) ||
count($query->clause('union')) ||
$query->clause('having')
);
if (!$complex) {
// Expression fields could have bound parameters.
foreach ($query->clause('select') as $field) {
if ($field instanceof ExpressionInterface) {
$complex = true;
break;
}
}
}
if (!$complex && $this->_valueBinder !== null) {
$order = $this->clause('order');
$complex = $order === null ? false : $order->hasNestedExpression();
}
$count = ['count' => $query->func()->count('*')];
if (!$complex) {
$query->getEagerLoader()->disableAutoFields();
$statement = $query
->select($count, true)
->disableAutoFields()
->execute();
} else {
$statement = $this->getConnection()->newQuery()
->select($count)
->from(['count_source' => $query])
->execute();
}
$result = $statement->fetch('assoc');
$statement->closeCursor();
if ($result === false) {
return 0;
}
return (int)$result['count'];
}
/**
* Registers a callable function that will be executed when the `count` method in
* this query is called. The return value for the function will be set as the
* return value of the `count` method.
*