コード例 #1
0
ファイル: ActiveRecordTest.php プロジェクト: howq/yii2
 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'));
 }
コード例 #2
0
ファイル: Customer.php プロジェクト: sciurodont/yii2
 public function getProfile()
 {
     return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
 }