M
M
Maxim Fedorov2017-10-04 13:34:00
Yii
Maxim Fedorov, 2017-10-04 13:34:00

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',
        ];
    }

how to get the category ? Link generated by standard gii tools,
all communication methods
/**
     * @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

1 answer(s)
L
Lander, 2017-10-04
@Maksclub

public function fields()
    {
        return [
            'id',
            'name',
            'phones',
            'building',
            'categories',
        ];
    }

Doesn't that work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question