Hi,
I have questions about code architecture between Yii1 and Yii2
in Yii 1 the model relation is like that
myModel extends CActiveRecord {
public function relations() {
return array(
'relatedModel' => array(self::BELONGS_TO, 'RelatedModel', 'some_id'),
)
}
}
And use it like that
$myModel->relatedModel
//or
myModel::model()->with('relatedModel')->find(...);
In Yii 2
myModel extends ActiveRecord {
function getRelatedModel() {
return $this->hasOne(RelatedModel::className(), ['some_id'=>'id'])
}
}
And use it like that
$myModel->relatedModel
or
myModel::find(...)->with('relatedModel')->one();
The advantages in Yii 1
according of getRelated method of CActiveRecord if we use $myModel->relatedModel more than of one time (for the same $myModel in the same php request) the relatedModel will not be loaded by sql query beacause is kept in the memory (by Yii 1 framework)
Now in Yii 2
if we user the $myModel->relatedModel many times Yii2 will runs the getRelatedModel() method using one() method
of ActiveQuery. In this way the same query will be executed many time in database reducing of performance
How to achieve this benefits of the Yii 1 in Yii 2 ?
Thanks