mysql - YII2 - Handle NULL value in gridView- Display a column of foreign keys where some may be NULL -
in brief, have table machines, has fk pointing subcategory.id relation correctly setup gii. however, fk column in machine can set null action field not compulsory.
[ 'attribute'=>'machine sub-category', 'value' => function ($model) { return $model->subcategory->subcat_name; }, ],
above code displays subcategory name when fk not null. if fk null, php error:
trying property of non-object
i understand error because of null value. ( because don't error if add value instead of null)
so query - fk column may have null values, how them display in gridview or detailview?
you can check if subcategory null
[ 'attribute'=>'machine sub-category', 'value' => function ($model) { if (isset($model->subcategory)){ return $model->subcategory->subcat_name; } else { return ''; } }, ],
Comments
Post a Comment