Commit 683a46d9 by Carsten Brandt

test for deeply nested relation

close #2543
parent 4ac784cd
<?php
namespace yiiunit\data\ar;
use yii\db\ActiveQuery;
use yiiunit\framework\db\ActiveRecordTest;
/**
......@@ -34,6 +35,17 @@ class Customer extends ActiveRecord
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
}
// deeply nested table relation
public function getOrderItems()
{
/** @var ActiveQuery $rel */
$rel = $this->hasMany(Item::className(), ['id' => 'item_id']);
return $rel->viaTable('tbl_order_item', ['order_id' => 'id'], function($q) {
/** @var ActiveQuery $q */
$q->viaTable('tbl_order', ['customer_id' => 'id']);
})->orderBy('id');
}
public function afterSave($insert)
{
ActiveRecordTest::$afterSaveInsert = $insert;
......
......@@ -131,6 +131,22 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(2, $order['books'][1]['id']);
}
// deeply nested table relation
public function testDeeplyNestedTableRelation()
{
/** @var Customer $customer */
$customer = $this->callCustomerFind(1);
$this->assertNotNull($customer);
$items = $customer->orderItems;
$this->assertEquals(2, count($items));
$this->assertInstanceOf(Item::className(), $items[0]);
$this->assertInstanceOf(Item::className(), $items[1]);
$this->assertEquals(1, $items[0]->id);
$this->assertEquals(2, $items[1]->id);
}
public function testStoreNull()
{
$record = new NullValues();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment