【发布时间】:2017-06-11 19:00:02
【问题描述】:
我使用外键从一个表中获取数据库值而不是另一个表,例如这个...
public function getAuthor() {
return $this->hasOne(SiteUsers::className(), ['id' => 'authorId']);
}
... 或 CRUD 视图文件中的匿名函数,例如:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'hotel_id' => [
'attribute' => 'hotel_id',
'value' => function ($value) {
return \common\models\Hotels::find()
->where(['id' => $value->hotel_id])
->one()['name'];
}
],
'country_id' => [
'attribute' => 'country_id',
'value' => function ($value) {
return \common\models\Countries::find()
->where(['id' => $value->country_id])
->one()['name'];
}
],
'room_type',
'max_persons',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
现在,问题是:一种方法比另一种更有效吗?为什么?
【问题讨论】:
标签: php database performance activerecord yii2