Reputation: 2724
I've written my code like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
'quantity',
[
'header' => 'SN',
'format' => 'raw',
'value' => function($data) {
$product = Product::findOne($data->product_ID);
return $product->SN_required ? '<span class="glyphicon glyphicon-ok"></span>' : '';
}
],
In order to show it like this:
Yet I think this is not correct (eventho it's working). Could someone please give me the correct notation for this sort of code?
I know this has to do with the model relations. That has been changed in Yii2.
public function getProduct()
{
return $this->hasOne(Product::className(), ['ID' => 'product_ID']);
}
Upvotes: 1
Views: 964
Reputation: 14860
You were on the right track. You can access relations inside the closure using $data->relation
in your case:
'value' => function($data) {
return $data->product->SN_required ? '<span class="glyphicon glyphicon-ok"></span>' : '';
}
You can view the Yii2 page on working with relations in data widgets for more information.
Upvotes: 1