Q:

Yii2 efficiency - foreign key or anonymous function in view?

I used the foreign key in order to obtain database values from one table over another, such as this...
public function getAuthor() {
    return $this->hasOne(SiteUsers::className(), ['id' => 'authorId']);
}

... or anonymous functions within the CRUD view files, such as:
    <?= 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'],
        ],
    ]); ?>

Now, the question is: is one method more efficient than the other? And why?

A:

You must keep in consideration that if you use activeRecord the relation  getAuthor()  is anyway  performed, and this is performed  for each model) involved  in dataProvider.  
In general the direct access is ever more fast that the ORM based access. And the access by anonymous function  performed   in rendering  èphase is substantially equivalentd to the access peformed  by relation .. the best performance are based on direct command  avoiding ORM  or activeRecord modelling. but this implies the lost of the level of abstraction granted by ORM.
Remember that if you have both (relation an anonymous function ) you perform the query two times .. 

