Browse Source

test for eager loading when there are 3 tables between the relation using via() to link it

ar-bug
Ezekiel Fernandez 10 years ago committed by Carsten Brandt
parent
commit
4c49f0f456
  1. 15
      tests/data/ar/Customer.php
  2. 11
      tests/framework/ar/ActiveRecordTestTrait.php

15
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');

11
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'));
}
}

Loading…
Cancel
Save