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