Yii2:通过多个via的关系

San*_*nky 5 php yii2

我通过多个中间表有关系.我怎样才能在Yii2中定义?

所以我试过以下

public function getTbl1()
{
    return $this->hasOne( Tbl1::className(), [ 'id' => 'tbl1_id' ] );
}

public function getTbl2()
{
    return $this->hasOne( Tbl2::className(), [ 'id' => 'tbl2_id' ] )->via( 'tbl1' );
}

public function getTbl3()
{
    return $this->hasOne( Tbl3::className(), [ 'id' => 'tbl3_id' ] )->via( 'tbl2' );
}
Run Code Online (Sandbox Code Playgroud)

我得到关系tbl1和tbl2,但不能得到tbl3.我该怎么做?

提前致谢.

Yup*_*pik 6

试过这个:

/**
 * @return ActiveQuery
 */
public function getLastPosition()
{
    return $this
                    ->hasOne(Position::class, ['equipment_id' => 'id'])
                    ->orderBy('date DESC');
}

/**
 * @return ActiveQuery
 */
public function getTest1()
{
    return $this->hasOne(CompanyCarpark::class, ['id' => 'company_carpark_id'])->via('lastPosition');
}

/**
 * @return ActiveQuery
 */
public function getTest2()
{
    return $this->hasOne(Company::class, ['id' => 'company_id'])->via('test1');
}
Run Code Online (Sandbox Code Playgroud)

它"像一个魅力".检查数据库中的密钥,可能会出现问题.