The model is pulled out without connections and they are loaded dynamically for each model, which adversely affects the performance.

I get the values ​​like this

/ rest / url-checker / 1? expand = patterns

The answer has this format

<response> <title>Test</title> <is_active>0</is_active> <patterns> <item> <url_checker_pattern_id>1</url_checker_pattern_id> <url_checker_id>1</url_checker_id> <pattern>test</pattern> <added_on>2016-10-18 16:31:24</added_on> </item> <item> <url_checker_pattern_id>4</url_checker_pattern_id> <url_checker_id>1</url_checker_id> <pattern>test2</pattern> <added_on>2016-10-18 16:31:35</added_on> </item> </patterns> </response> 

It is also not clear how to pull out only certain fields and not all for relation. Since I only need the value of pattern

 <patterns> <item> <pattern>test</pattern> </item> <item> <pattern>test2</pattern> </item> </patterns> 

Controller

 class UrlCheckerController extends ActiveController { public $modelClass = 'backend\modules\rest\models\UrlChecker'; } 

Model

 class UrlChecker extends ActiveRecord { private $_patterns; /** * @return \yii\db\ActiveQuery */ public function getPatterns() { return $this->hasMany(UrlCheckerPattern::className(), ['url_checker_id' => 'url_checker_id']); } public function setPatterns($values) { $this->_patterns = $values; } /** * @inheritdoc */ public function afterSave($insert, $changedAttributes) { if (!$insert) { UrlCheckerPattern::deleteAll([ 'url_checker_id' => $this->url_checker_id, ]); } foreach ($this->_patterns as $pattern) { $relation = new UrlCheckerPattern(); $relation->url_checker_id = $this->url_checker_id; $relation->pattern = $pattern; if (!$relation->save()) { throw new Exception('Relation don\'t save'); } } parent::afterSave($insert, $changedAttributes); } /** * @inheritdoc */ public function fields() { return [ 'title', 'is_active', 'test' => function ($model) { /** * @var UrlChecker $model * Тут пусто */ return $model->relatedRecords; }, /*'patterns' => function ($model) { $output = []; foreach ($model->patterns as $pattern) { $output[] = [ 'pattern' => $pattern->pattern, ]; } return $output; },*/ ]; } /** * @inheritdoc */ public function extraFields() { return [ 'patterns', ]; } } 

    1 answer 1

    I advise you to read the yii2 documentation on REST web services: https://github.com/yiisoft/yii2/blob/master/docs/guide-ru/README.md

    To display a specific model with preloaded data without additional requests, you need to override the actionView($id) method actionView($id) in the controller:

     public function actionView($id) { // с помощью with() будет "жадная" загрузка данных, т.е. модель подгрузится сразу с указанными связями return UrlChecker::find()->where(['id' => $id])->with('patterns')->one(); } 

    The code did not check, but it should work. Selecting only one "pattern" field will not greatly increase your productivity, but if you want it so much, then you need to work with ActiveQuery and do join manually.

    ps: changed the record, in the first one there was an error due to inattention. If the new version does not help, then unsubscribe