我通过多个中间表有关系.我怎样才能在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.我该怎么做?
提前致谢.
试过这个:
/**
* @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)
它"像一个魅力".检查数据库中的密钥,可能会出现问题.
| 归档时间: |
|
| 查看次数: |
2443 次 |
| 最近记录: |