From 4c49f0f456e2f87f5793a0c2801c8855630d443a Mon Sep 17 00:00:00 2001 From: Ezekiel Fernandez Date: Sun, 8 Mar 2015 10:50:34 +0800 Subject: [PATCH] test for eager loading when there are 3 tables between the relation using via() to link it --- tests/data/ar/Customer.php | 15 +++++++++++++++ tests/framework/ar/ActiveRecordTestTrait.php | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/tests/data/ar/Customer.php b/tests/data/ar/Customer.php index a86fe7f..789e5e5 100644 --- a/tests/data/ar/Customer.php +++ b/tests/data/ar/Customer.php @@ -44,6 +44,21 @@ class Customer extends ActiveRecord return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id'); } + public function getOrderItems2() + { + return $this->hasMany(OrderItem::className(), ['order_id' => 'id'])->via('orders'); + } + + public function getItems() + { + return $this->hasMany(Item::className(), ['id' => 'item_id'])->via('orderItems2'); + } + + public function getCategories() + { + return $this->hasMany(Category::className(), ['id' => 'category_id'])->via('items'); + } + public function getExpensiveOrders() { return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id'); diff --git a/tests/framework/ar/ActiveRecordTestTrait.php b/tests/framework/ar/ActiveRecordTestTrait.php index 9334838..7eaf4c5 100644 --- a/tests/framework/ar/ActiveRecordTestTrait.php +++ b/tests/framework/ar/ActiveRecordTestTrait.php @@ -1190,4 +1190,15 @@ trait ActiveRecordTestTrait $this->assertTrue(isset($items[4])); $this->assertTrue(isset($items[5])); } + + public function testEagerLoadingWithViaRelation() + { + /* @var $this TestCase|ActiveRecordTestTrait */ + + /* @var $customerClass \yii\db\ActiveRecordInterface */ + $customerClass = $this->getCustomerClass(); + /** @var $customer \yii\db\ActiveRecord $customer */ + $customer = $customerClass::find()->where(['id' => 1])->with(['categories'])->one(); + $this->assertTrue($customer->isRelationPopulated('categories')); + } }