100
100
* as inverse of another relation and {@see onCondition()} which adds a condition that is to be added to relational
101
101
* query join condition.
102
102
*
103
- * @psalm-import-type ARClass from ActiveQueryInterface
103
+ * @psalm-import-type ModelClass from ActiveQueryInterface
104
104
* @psalm-import-type IndexKey from ArArrayHelper
105
105
*
106
106
* @psalm-property IndexKey|null $indexBy
@@ -116,12 +116,12 @@ class ActiveQuery extends Query implements ActiveQueryInterface
116
116
private array $ joinWith = [];
117
117
118
118
/**
119
- * @psalm-param ARClass $arClass
119
+ * @psalm-param ModelClass $modelClass
120
120
*/
121
121
final public function __construct (
122
- protected string |ActiveRecordInterface |Closure $ arClass
122
+ protected string |ActiveRecordModelInterface |Closure $ modelClass
123
123
) {
124
- parent ::__construct ($ this ->getARInstance ()->db ());
124
+ parent ::__construct ($ this ->getModelInstance ()->db ());
125
125
}
126
126
127
127
public function each (): DataReaderInterface
@@ -169,6 +169,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
169
169
if ($ this ->primaryModel === null ) {
170
170
$ query = $ this ->createInstance ();
171
171
} else {
172
+ $ activeRecord = $ this ->primaryModel ->activeRecord ();
172
173
$ where = $ this ->getWhere ();
173
174
174
175
if ($ this ->via instanceof ActiveQueryInterface) {
@@ -183,21 +184,21 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
183
184
if ($ viaQuery ->getMultiple ()) {
184
185
if ($ viaCallableUsed ) {
185
186
$ viaModels = $ viaQuery ->all ();
186
- } elseif ($ this -> primaryModel ->isRelationPopulated ($ viaName )) {
187
- /** @var ActiveRecordInterface []|array[] $viaModels */
188
- $ viaModels = $ this -> primaryModel ->relation ($ viaName );
187
+ } elseif ($ activeRecord ->isRelationPopulated ($ viaName )) {
188
+ /** @var ActiveRecordModelInterface []|array[] $viaModels */
189
+ $ viaModels = $ activeRecord ->relation ($ viaName );
189
190
} else {
190
191
$ viaModels = $ viaQuery ->all ();
191
- $ this -> primaryModel ->populateRelation ($ viaName , $ viaModels );
192
+ $ activeRecord ->populateRelation ($ viaName , $ viaModels );
192
193
}
193
194
} else {
194
195
if ($ viaCallableUsed ) {
195
196
$ model = $ viaQuery ->one ();
196
- } elseif ($ this -> primaryModel ->isRelationPopulated ($ viaName )) {
197
- $ model = $ this -> primaryModel ->relation ($ viaName );
197
+ } elseif ($ activeRecord ->isRelationPopulated ($ viaName )) {
198
+ $ model = $ activeRecord ->relation ($ viaName );
198
199
} else {
199
200
$ model = $ viaQuery ->one ();
200
- $ this -> primaryModel ->populateRelation ($ viaName , $ model );
201
+ $ activeRecord ->populateRelation ($ viaName , $ model );
201
202
}
202
203
$ viaModels = $ model === null ? [] : [$ model ];
203
204
}
@@ -228,8 +229,8 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
228
229
* @psalm-param list<array> $rows
229
230
* @psalm-return (
230
231
* $rows is non-empty-list<array>
231
- * ? non-empty-list<ActiveRecordInterface |array>
232
- * : list<ActiveRecordInterface |array>
232
+ * ? non-empty-list<ActiveRecordModelInterface |array>
233
+ * : list<ActiveRecordModelInterface |array>
233
234
* )
234
235
*/
235
236
public function populate (array $ rows ): array
@@ -274,8 +275,8 @@ public function populate(array $rows): array
274
275
*/
275
276
private function removeDuplicatedRows (array $ rows ): array
276
277
{
277
- $ instance = $ this ->getARInstance ();
278
- $ pks = $ instance ->primaryKey ();
278
+ $ instance = $ this ->getModelInstance ();
279
+ $ pks = $ instance ->activeRecord ()-> primaryKey ();
279
280
280
281
if (empty ($ pks )) {
281
282
throw new InvalidConfigException ('Primary key of " ' . $ instance ::class . '" can not be empty. ' );
@@ -301,7 +302,7 @@ private function removeDuplicatedRows(array $rows): array
301
302
return array_values (array_combine ($ hash , $ rows ));
302
303
}
303
304
304
- public function one (): array |ActiveRecordInterface |null
305
+ public function one (): array |ActiveRecordModelInterface |null
305
306
{
306
307
if ($ this ->shouldEmulateExecution ()) {
307
308
return null ;
@@ -417,10 +418,10 @@ public function buildJoinWith(): void
417
418
418
419
$ this ->join = [];
419
420
420
- $ arClass = $ this ->getARInstance ();
421
+ $ model = $ this ->getModelInstance ();
421
422
422
423
foreach ($ this ->joinWith as [$ with , $ eagerLoading , $ joinType ]) {
423
- $ this ->joinWithRelations ($ arClass , $ with , $ joinType );
424
+ $ this ->joinWithRelations ($ model , $ with , $ joinType );
424
425
425
426
if (is_array ($ eagerLoading )) {
426
427
foreach ($ with as $ name => $ callback ) {
@@ -480,7 +481,7 @@ public function innerJoinWith(array|string $with, array|bool $eagerLoading = tru
480
481
/**
481
482
* Modifies the current query by adding join fragments based on the given relations.
482
483
*
483
- * @param ActiveRecordInterface $arClass The primary model.
484
+ * @param ActiveRecordModelInterface $model The primary model.
484
485
* @param array $with The relations to be joined.
485
486
* @param array|string $joinType The join type.
486
487
*
@@ -489,7 +490,7 @@ public function innerJoinWith(array|string $with, array|bool $eagerLoading = tru
489
490
* @throws NotInstantiableException
490
491
* @throws \Yiisoft\Definitions\Exception\InvalidConfigException
491
492
*/
492
- private function joinWithRelations (ActiveRecordInterface $ arClass , array $ with , array |string $ joinType ): void
493
+ private function joinWithRelations (ActiveRecordModelInterface $ model , array $ with , array |string $ joinType ): void
493
494
{
494
495
$ relations = [];
495
496
@@ -499,7 +500,7 @@ private function joinWithRelations(ActiveRecordInterface $arClass, array $with,
499
500
$ callback = null ;
500
501
}
501
502
502
- $ primaryModel = $ arClass ;
503
+ $ primaryModel = $ model ;
503
504
$ parent = $ this ;
504
505
$ prefix = '' ;
505
506
@@ -518,7 +519,7 @@ private function joinWithRelations(ActiveRecordInterface $arClass, array $with,
518
519
}
519
520
520
521
if ($ relation instanceof ActiveQueryInterface) {
521
- $ primaryModel = $ relation ->getARInstance ();
522
+ $ primaryModel = $ relation ->getModelInstance ();
522
523
$ parent = $ relation ;
523
524
}
524
525
@@ -564,7 +565,7 @@ private function getJoinType(array|string $joinType, string $name): string
564
565
}
565
566
566
567
/**
567
- * Returns the table name and the table alias for {@see arClass }.
568
+ * Returns the table name and the table alias for {@see $modelClass }.
568
569
*
569
570
* @throws CircularReferenceException
570
571
* @throws InvalidConfigException
@@ -733,9 +734,9 @@ public function orOnCondition(array|string $condition, array $params = []): stat
733
734
734
735
public function viaTable (string $ tableName , array $ link , callable $ callable = null ): static
735
736
{
736
- $ arClass = $ this ->primaryModel ?? $ this ->arClass ;
737
+ $ modelClass = $ this ->primaryModel ?? $ this ->modelClass ;
737
738
738
- $ relation = (new static ($ arClass ))
739
+ $ relation = (new static ($ modelClass ))
739
740
->from ([$ tableName ])
740
741
->link ($ link )
741
742
->multiple (true )
@@ -791,7 +792,7 @@ public function getTablesUsedInFrom(): array
791
792
*/
792
793
protected function getPrimaryTableName (): string
793
794
{
794
- return $ this ->getARInstance ()->getTableName ();
795
+ return $ this ->getModelInstance ()->tableName ();
795
796
}
796
797
797
798
public function getOn (): array |string |null
@@ -812,9 +813,9 @@ public function getSql(): string|null
812
813
return $ this ->sql ;
813
814
}
814
815
815
- public function getARClass (): string |ActiveRecordInterface |Closure
816
+ public function getModelClass (): string |ActiveRecordModelInterface |Closure
816
817
{
817
- return $ this ->arClass ;
818
+ return $ this ->modelClass ;
818
819
}
819
820
820
821
/**
@@ -823,7 +824,7 @@ public function getARClass(): string|ActiveRecordInterface|Closure
823
824
* @throws InvalidConfigException
824
825
* @throws Throwable
825
826
*/
826
- public function findOne (mixed $ condition ): array |ActiveRecordInterface |null
827
+ public function findOne (mixed $ condition ): array |ActiveRecordModelInterface |null
827
828
{
828
829
return $ this ->findByCondition ($ condition )->one ();
829
830
}
@@ -855,21 +856,22 @@ public function findAll(mixed $condition): array
855
856
*/
856
857
protected function findByCondition (mixed $ condition ): static
857
858
{
858
- $ arInstance = $ this ->getARInstance ();
859
+ $ modelInstance = $ this ->getModelInstance ();
860
+ $ activeRecord = $ modelInstance ->activeRecord ();
859
861
860
862
if (!is_array ($ condition )) {
861
863
$ condition = [$ condition ];
862
864
}
863
865
864
866
if (!DbArrayHelper::isAssociative ($ condition )) {
865
867
/** query by primary key */
866
- $ primaryKey = $ arInstance ->primaryKey ();
868
+ $ primaryKey = $ activeRecord ->primaryKey ();
867
869
868
870
if (isset ($ primaryKey [0 ])) {
869
871
$ pk = $ primaryKey [0 ];
870
872
871
873
if (!empty ($ this ->getJoins ()) || !empty ($ this ->getJoinWith ())) {
872
- $ pk = $ arInstance -> getTableName () . '. ' . $ pk ;
874
+ $ pk = $ modelInstance -> tableName () . '. ' . $ pk ;
873
875
}
874
876
875
877
/**
@@ -878,11 +880,11 @@ protected function findByCondition(mixed $condition): static
878
880
*/
879
881
$ condition = [$ pk => array_values ($ condition )];
880
882
} else {
881
- throw new InvalidConfigException ('" ' . $ arInstance ::class . '" must have a primary key. ' );
883
+ throw new InvalidConfigException ('" ' . $ modelInstance ::class . '" must have a primary key. ' );
882
884
}
883
885
} else {
884
- $ aliases = $ arInstance ->filterValidAliases ($ this );
885
- $ condition = $ arInstance ->filterCondition ($ condition , $ aliases );
886
+ $ aliases = $ activeRecord ->filterValidAliases ($ this );
887
+ $ condition = $ activeRecord ->filterCondition ($ condition , $ aliases );
886
888
}
887
889
888
890
return $ this ->setWhere ($ condition );
@@ -905,18 +907,18 @@ public function sql(string|null $value): static
905
907
return $ this ;
906
908
}
907
909
908
- public function getARInstance (): ActiveRecordInterface
910
+ public function getModelInstance (): ActiveRecordModelInterface
909
911
{
910
- if ($ this ->arClass instanceof ActiveRecordInterface ) {
911
- return clone $ this ->arClass ;
912
+ if ($ this ->modelClass instanceof ActiveRecordModelInterface ) {
913
+ return clone $ this ->modelClass ;
912
914
}
913
915
914
- if ($ this ->arClass instanceof Closure) {
915
- return ($ this ->arClass )();
916
+ if ($ this ->modelClass instanceof Closure) {
917
+ return ($ this ->modelClass )();
916
918
}
917
919
918
- /** @psalm-var class-string<ActiveRecordInterface > $class */
919
- $ class = $ this ->arClass ;
920
+ /** @psalm-var class-string<ActiveRecordModelInterface > $class */
921
+ $ class = $ this ->modelClass ;
920
922
921
923
return new $ class ();
922
924
}
@@ -928,7 +930,7 @@ protected function index(array $rows): array
928
930
929
931
private function createInstance (): static
930
932
{
931
- return (new static ($ this ->arClass ))
933
+ return (new static ($ this ->modelClass ))
932
934
->where ($ this ->getWhere ())
933
935
->limit ($ this ->getLimit ())
934
936
->offset ($ this ->getOffset ())
@@ -946,7 +948,7 @@ private function createInstance(): static
946
948
->withQueries ($ this ->withQueries );
947
949
}
948
950
949
- private function populateOne (array $ row ): ActiveRecordInterface |array
951
+ private function populateOne (array $ row ): ActiveRecordModelInterface |array
950
952
{
951
953
return $ this ->populate ([$ row ])[0 ];
952
954
}
0 commit comments