- Scenario
- Behavior in `yii\widgets\DetailView
- Behavior in `kartik\detail\DetailView
- Reasoning & Solution
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.
Scenario ¶
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' => [
'id',
'book_name',
'author.name'
]
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' => [
'id',
'book_name',
[
'attribute'=>'author_id',
'value'=>$model->author->name,
'type'=>DetailView::INPUT_SELECT2,
'widgetOptions'=>[
'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.
display multiple attributes
Hi,
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.
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.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.