I’ve followed the example in the yii2 guide to create related models using a junction table.
What I have now are these 3 models/tables :
class Order extends \yii\db\ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), ['order_id' => 'order_id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['item_id' => 'item_id'])
->via('orderItems');
}
}
class Item extends \yii\db\ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), ['item_id' => 'item_id']);
}
public function getOrders()
{
return $this->hasMany(Order::className(), ['order_id' => 'order_id'])
->via('orderItems');
}
}
class OrderItem extends \yii\db\ActiveRecord
{
public function getOrder()
{
return $this->hasOne(Order::className(), ['order_id' => 'order_id']);
}
public function getItem()
{
return $this->hasOne(Item::className(), ['item_id' => 'item_id']);
}
}
What I want to do now is display a GridView of the related items
in the view.php
view file of the selected Order
along with an ActionColumn with buttons to view the item and to delete the orderItem (by calling the appropriate action inside their respective controllers).
So now I have the following configuration in order\view.php:
/* @var $model app\models\Order */
$dataProvider = new ActiveDataProvider([
'query' => $model->getItems(),
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'item_id',
'name',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {delete}',
'buttons' => [
'view' => function ($url, $item, $key) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['item/view', 'id' => $item->item_id]);
},
'delete' => function ($url, $item, $key) {
return Html::a('<span class="glyphicon glyphicon-remove-sign"></span>', ['order-item/delete', 'id' => $item->getOrderItems()->where(['order_id' => $model->order_id])->one()->order_item_id]);
},
],
],
],
])
With this code I get a Undefined variable: model
error message because it seems the $model
variable is undefined inside the scope of the callback function.
How can I resolve this issue? Any help is appreciated.