我正在维护一个包含模型和子记录网格的表单。我希望网格显示孩子的网址,而不是父网址。
我有两个共享父子关系的数据库表。我只展示了重要的领域。
describe ops;
+---------------+--------------+
| serial_number | int(11) |
+---------------+--------------+
describe opsitem;
+---------------+--------------+
| opsitem_id | int(11) |
| ops_id | int(11) | # foreign key
| serial_number | int(11) |
+---------------+--------------+
在我的控制器中,我显示Ops的表单,然后为子记录创建一个activeRecord - Opsitem
class OpsController extends Controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->ops_id]);
} else {
$searchModel = new OpsitemSearch();
$dataProvider = $searchModel->search(
['OpsitemSearch' => ['ops_id' => $model->ops_id]]
);
return $this->render('update', [
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
}
我的表单包含ops(父级)的表单字段,然后是包含oppsitem(子)记录的网格
// $model is Parent - Ops
<?php echo $this->render('_form', [
'model' => $model,
]) ?>
// $searchModel is Opsitem - Child
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'ops_item_id',
'ops_id',
'serial_number'
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update}{delete}',
],
],
]); ?>
点击网格上的“更新”按钮会将我指向url
:
/操作/更新?ID = 1234
我想要
/ opsitem /更新?ID = 1234
答案 0 :(得分:2)
您需要为变更控制器添加controller
属性。
像,
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'ops_item_id',
'ops_id',
'serial_number'
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update}{delete}',
'controller' => 'opsitem',
],
],
]); ?>
答案 1 :(得分:1)
您可以自定义gridview
按钮。例如,
'template' => '{update} {delete}',
'buttons' => [
'update' => function ($url, $model) {
return Html::a('Update',\Yii::$app->getUrlManager()->createUrl(['/opsitem/update', 'id' => 1234]),['class' => 'any class']);
},
],