We are going to deploy the new yiiframework.com website on March 23, 2018 from 8:00 to 12:00 UTC.
Website and Yii documentation will not be available in this time frame. Check the news for more details.

Yii 2.0: Relational data fields display and edit with the yii2-detail-view extension


If you have read this wiki you would be aware of the yii2-detail-view extension and how it offers you an easy way to EDIT and VIEW your model data, toggle these modes, and other features. You can read the extension documentation and/or view a demo of this extension, to understand usage.

Now if you are using the yii2-detail-view to display relational data, you must note that there is a slight difference in usage of attributes than in a normal yii detail-view.


For example, let's consider you want to display the model Book in the detail view, which has an attribute author_id. The Book model has a relation named author to the author.name field linked via author_id.

Behavior in `yii\widgets\DetailView

For the core framework yii\widgets\DetailView, you can simply have the DetailView attributes set like below

'attributes' => [

Behavior in `kartik\detail\DetailView

However, in the yii2-detail-view extension, a configuration like above will throw the following exception.

Exception: The attribute 'author.name' is invalid. You cannot directly pass relational attributes in string format within '\kartik\widgets\DetailView'. Instead use the array format with 'attribute' property set to base field, and the 'value' property returning the relational data.

Reasoning & Solution

The yii2-detail-view is built to also display attributes for editing, and there is no base table attribute in the model called author.name. The solution to this is simple. Always use array format to pass such relational attributes. So the simple configuration for the kartik\detail\DetailView is the following:

'attributes' => [
      'data'=>ArrayHelper::map(Author::find()->orderBy('name')->asArray()->all(), 'id', 'name'),

Now that should resolve both the VIEW and EDIT issues for you. With the above configuration, users would still see the author name instead of author_id in both VIEW and EDIT modes. In the EDIT mode, the above configuration will internally use the author_id to save data, but display the author names as a dropdown list using the \kartik\widgets\Select2 widget.

Total 2 comments

#19665 report it
sifa786 at 2015/11/16 07:18am
What about multiple = true? I cannot get it to work

Please give an example for DetailView? I can get it working just find standalone, but run into all kinds of errors within DetailView. My code below ...

                'attribute' => 'MyFieldName', //this is a field i declared on the model manually
                'type' => DetailView::INPUT_SELECT2,
                'values' => isset($model->MyFieldName) ? [array_values($model->MyFieldName)] : [],
                'options' => [
                    'multiple' => true,
                'widgetOptions' => [
                    'data'=>ArrayHelper::map(LUTargetSpecies::find()->orderBy('DisplayName')->asArray()->all(), 'LookupID', 'DisplayName'),

I set $model->MyFieldName to ['4'=>'4', '5'=>'5'] beforehand for testing. I get error :htmlspecialchars() expects parameter 1 to be string, array given

If I just set it to 4 or 5, it works with the single value.

#19302 report it
anoel at 2015/05/15 10:33pm
display multiple attributes


is there a way to show multiple attributes on the same row? At least display the View in two columns otherwise the pages become infinite with lots of data to scroll.

Thnak you.

Leave a comment

Please to leave your comment.

Write new article