Example #1
0
 public function testJoinWithAndScope()
 {
     // hasOne inner join
     $customers = Customer::find()->active()->innerJoinWith('profile')->orderBy('customer.id')->all();
     $this->assertEquals(1, count($customers));
     $this->assertEquals(1, $customers[0]->id);
     $this->assertTrue($customers[0]->isRelationPopulated('profile'));
     // hasOne outer join
     $customers = Customer::find()->active()->joinWith('profile')->orderBy('customer.id')->all();
     $this->assertEquals(2, count($customers));
     $this->assertEquals(1, $customers[0]->id);
     $this->assertEquals(2, $customers[1]->id);
     $this->assertTrue($customers[0]->isRelationPopulated('profile'));
     $this->assertTrue($customers[1]->isRelationPopulated('profile'));
     $this->assertInstanceOf(Profile::className(), $customers[0]->profile);
     $this->assertNull($customers[1]->profile);
     // hasMany
     $customers = Customer::find()->active()->joinWith(['orders' => function ($q) {
         $q->orderBy('order.id');
     }])->orderBy('customer.id DESC, order.id')->all();
     $this->assertEquals(2, count($customers));
     $this->assertEquals(2, $customers[0]->id);
     $this->assertEquals(1, $customers[1]->id);
     $this->assertTrue($customers[0]->isRelationPopulated('orders'));
     $this->assertTrue($customers[1]->isRelationPopulated('orders'));
 }
Example #2
0
 public function getProfile()
 {
     return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
 }