diff --git a/tests/data/ar/Order.php b/tests/data/ar/Order.php index 235ad13..29e1167 100644 --- a/tests/data/ar/Order.php +++ b/tests/data/ar/Order.php @@ -24,6 +24,18 @@ class Order extends ActiveRecord return $this->hasOne(Customer::className(), ['id' => 'customer_id']); } + public function getCustomerJoinedWithProfile() + { + return $this->hasOne(Customer::className(), ['id' => 'customer_id']) + ->joinWith('profile'); + } + + public function getCustomerJoinedWithProfileIndexOrdered() + { + return $this->hasMany(Customer::className(), ['id' => 'customer_id']) + ->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name'); + } + public function getCustomer2() { return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->inverseOf('orders2'); diff --git a/tests/data/ar/OrderItem.php b/tests/data/ar/OrderItem.php index ebb590c..b1dbe78 100644 --- a/tests/data/ar/OrderItem.php +++ b/tests/data/ar/OrderItem.php @@ -28,4 +28,15 @@ class OrderItem extends ActiveRecord { return $this->hasOne(Item::className(), ['id' => 'item_id']); } + + // relations used by ::testFindCompositeWithJoin() + public function getOrderItemCompositeWithJoin() + { + return $this->hasOne(OrderItem::className(), ['item_id' => 'item_id', 'order_id' => 'order_id' ]) + ->joinWith('item'); + } + public function getOrderItemCompositeNoJoin() + { + return $this->hasOne(OrderItem::className(), ['item_id' => 'item_id', 'order_id' => 'order_id' ]); + } } diff --git a/tests/framework/db/ActiveRecordTest.php b/tests/framework/db/ActiveRecordTest.php index 7a9ad25..59528b5 100644 --- a/tests/framework/db/ActiveRecordTest.php +++ b/tests/framework/db/ActiveRecordTest.php @@ -815,6 +815,39 @@ class ActiveRecordTest extends DatabaseTestCase $this->assertEquals(0, count($orders[0]->itemsIndexed)); } + /** + * https://github.com/yiisoft/yii2/issues/10201 + * https://github.com/yiisoft/yii2/issues/9047 + */ + public function testFindCompositeRelationWithJoin() + { + /* @var $orderItem OrderItem */ + $orderItem = OrderItem::findOne([1, 1]); + + $orderItemNoJoin = $orderItem->orderItemCompositeNoJoin; + $this->assertInstanceOf('yiiunit\data\ar\OrderItem', $orderItemNoJoin); + + $orderItemWithJoin = $orderItem->orderItemCompositeWithJoin; + $this->assertInstanceOf('yiiunit\data\ar\OrderItem', $orderItemWithJoin); + } + + public function testFindSimpleRelationWithJoin() + { + /* @var $order Order */ + $order = Order::findOne(1); + + $customerNoJoin = $order->customer; + $this->assertInstanceOf('yiiunit\data\ar\Customer', $customerNoJoin); + + $customerWithJoin = $order->customerJoinedWithProfile; + $this->assertInstanceOf('yiiunit\data\ar\Customer', $customerWithJoin); + + $customerWithJoinIndexOrdered = $order->customerJoinedWithProfileIndexOrdered; + $this->assertTrue(is_array($customerWithJoinIndexOrdered)); + $this->assertArrayHasKey('user1', $customerWithJoinIndexOrdered); + $this->assertInstanceOf('yiiunit\data\ar\Customer', $customerWithJoinIndexOrdered['user1']); + } + public function tableNameProvider() { return [