Yii2: attitude after a few through

I have a relationship through several staging tables. How to identify in Yii2?

So I tried to follow

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' );
}

I get the ratio of tbl1 and tbl2 but cannot get tbl3. How can i do this?

Thanks in advance.

+4
source share
2 answers

Just tried this:

/**
 * @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');
}

And that "worked like a charm." Check your keys in the database, maybe there is something wrong.

+4
source

You must do this using the parent-child structure, for example:

$model->find()
 ->with('relationOne') //Model1::getRelationOne(Model2::table_name()...)
 ->with('relationOne.childRelation'); // Model2::getChildRelation....
0
source

Source: https://habr.com/ru/post/1663323/


All Articles