R
R
rommcr2015-02-19 17:59:54
Yii
rommcr, 2015-02-19 17:59:54

Yii2: How to get extraFields from associated data model?

There are two AR models: A,B. Model B connects to model A via hasOne (iata field):

 public function getIata()
    {
        return $this->hasOne(B::className(),
                        ['iata_id' => 'iata_id']);
    }

Model B has an extraField f:
    public function extraFields()
    {
        return ['f' => function(){ return "Something"; }];
    }

I need, when selecting an instance of model "A", to get the associated model "B" with the field "F".
Option
$a = A::findOne(1)->toArray([], ['iata', 'f', 'iata.f', 'F'], true);

Does not help. The iata field returns the associated model B without the additional field.
How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vyachin, 2015-06-04
@vyachin

read here https://github.com/yiisoft/yii2/blob/master/docs/g... assignment of extraFields - an indication of which relations to return to the Rest controller
And you will have to write the conversion to an array yourself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question