Answer the question
In order to leave comments, you need to log in
How to display a one-to-many relationship in the fields() of a model?
There are 3 models ( Building , Firm and Category )
I display in the fields of the Firm model the associated Building model like this:
public function fields()
{
return [
'id',
'name',
'phones',
'building',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBuilding()
{
return $this->hasOne(Building::className(), ['id' => 'building_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getFirmCategories()
{
return $this->hasMany(FirmCategory::className(), ['firm_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategories()
{
return $this->hasMany(Category::className(), ['id' => 'category_id'])->viaTable('{{%firm_category}}', ['firm_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question