From 08dc0399e9fd2d08d3b3244772dc85dc647cbf2e Mon Sep 17 00:00:00 2001 From: batyrmastyr Date: Fri, 20 Mar 2026 00:15:39 +0400 Subject: [PATCH 1/6] Relation query should be created by related class, not primary model class --- src/AbstractActiveRecord.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 878693f6b..26aa975ae 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -63,7 +63,9 @@ abstract class AbstractActiveRecord implements ActiveRecordInterface public function createQuery(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface { - return static::query($modelClass ?? $this); + $source = $modelClass ?? $this; + + return $source::query($source); } public function delete(): int From 2d551bbe9b78476915e5bcc05063493bb97f59e5 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Mon, 20 Apr 2026 12:29:39 +0700 Subject: [PATCH 2/6] Change query condition to use andWhere for orders --- tests/ActiveQueryFindTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveQueryFindTest.php b/tests/ActiveQueryFindTest.php index 53040c159..ea88033a8 100644 --- a/tests/ActiveQueryFindTest.php +++ b/tests/ActiveQueryFindTest.php @@ -577,7 +577,7 @@ public function testFindLazy(): void $customer = $customerQuery->findByPk(2); $this->assertFalse($customer->isRelationPopulated('orders')); - $orders = $customer->getOrdersQuery()->where(['id' => 3])->all(); + $orders = $customer->getOrdersQuery()->andWhere(['id' => 3])->all(); $this->assertFalse($customer->isRelationPopulated('orders')); $this->assertCount(0, $customer->relatedRecords()); $this->assertCount(1, $orders); From 67c68337098cf7db32bd26725e5d7a74f505e4e2 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Mon, 20 Apr 2026 12:31:08 +0700 Subject: [PATCH 3/6] Use TableNameAndAliasResolver for query aliasing --- src/Event/Handler/SoftDelete.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Event/Handler/SoftDelete.php b/src/Event/Handler/SoftDelete.php index a15513fdd..a82813396 100644 --- a/src/Event/Handler/SoftDelete.php +++ b/src/Event/Handler/SoftDelete.php @@ -8,6 +8,7 @@ use DateTimeImmutable; use Yiisoft\ActiveRecord\Event\AfterCreateQuery; use Yiisoft\ActiveRecord\Event\BeforeDelete; +use Yiisoft\ActiveRecord\Internal\TableNameAndAliasResolver; use Yiisoft\Db\QueryBuilder\Condition\Equals; use function is_callable; @@ -46,11 +47,11 @@ public function getEventHandlers(): array private function afterCreateQuery(AfterCreateQuery $event): void { $model = $event->model; - $tableName = $model->tableName(); + [, $alias] = TableNameAndAliasResolver::resolve($event->query); foreach ($this->getPropertyNames() as $propertyName) { if ($model->hasProperty($propertyName)) { - $event->query->andWhere(new Equals("$tableName.$propertyName", null)); + $event->query->andWhere(new Equals("$alias.$propertyName", null)); } } } From d3fc3c7e36b63b22ceafe9600246842762c701e8 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 2 May 2026 16:31:06 +0700 Subject: [PATCH 4/6] Revert last commit --- src/Event/Handler/SoftDelete.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Event/Handler/SoftDelete.php b/src/Event/Handler/SoftDelete.php index a82813396..a15513fdd 100644 --- a/src/Event/Handler/SoftDelete.php +++ b/src/Event/Handler/SoftDelete.php @@ -8,7 +8,6 @@ use DateTimeImmutable; use Yiisoft\ActiveRecord\Event\AfterCreateQuery; use Yiisoft\ActiveRecord\Event\BeforeDelete; -use Yiisoft\ActiveRecord\Internal\TableNameAndAliasResolver; use Yiisoft\Db\QueryBuilder\Condition\Equals; use function is_callable; @@ -47,11 +46,11 @@ public function getEventHandlers(): array private function afterCreateQuery(AfterCreateQuery $event): void { $model = $event->model; - [, $alias] = TableNameAndAliasResolver::resolve($event->query); + $tableName = $model->tableName(); foreach ($this->getPropertyNames() as $propertyName) { if ($model->hasProperty($propertyName)) { - $event->query->andWhere(new Equals("$alias.$propertyName", null)); + $event->query->andWhere(new Equals("$tableName.$propertyName", null)); } } } From 83b46b11005ff89e8f35d770a7e5ce88be7cc274 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 2 May 2026 19:54:56 +0700 Subject: [PATCH 5/6] Update CHANGELOG [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 697709beb..dbae1dd9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Enh #564: Clarify `$relations` parameter type in `JoinWith::__construct()` from `array` to `array` (@vjik) - Bug #561: Fix `ActiveRecordInterface::upsert()` with `$updateProperties = false` (@Tigrov) +- Bug #550: Relation query should be created by related class, not primary model class (@batyrmastyr) ## 1.0.2 March 11, 2026 From 4744a8117838a44e28f3f85155baf7a28123d1a3 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 2 May 2026 20:03:35 +0700 Subject: [PATCH 6/6] Clear variable name --- src/AbstractActiveRecord.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 26aa975ae..9be9d6a16 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -63,9 +63,9 @@ abstract class AbstractActiveRecord implements ActiveRecordInterface public function createQuery(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface { - $source = $modelClass ?? $this; + $modelClass ??= $this; - return $source::query($source); + return $modelClass::query($modelClass); } public function delete(): int