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 diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 878693f6b..9be9d6a16 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); + $modelClass ??= $this; + + return $modelClass::query($modelClass); } public function delete(): int 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);