当我打开更新"页面"我有动态行保存在数据库中。但我如何设法删除 actionUpdate 中的行以在DB中删除它们。已删除的项目位于数组中,其ID为已删除ID
public function actionUpdate($id) {
$model = Component::find()->where(['id' => $id])->one();
$depModels = Dependency::find()->where(['component_id' => $id])->all();
$tractorModels = ArrayHelper::map(Tractormodel::find()->all(), 'id', 'model');
$components = Component::find()->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$dependendComponents = Yii::$app->request->bodyParams['ids'];
foreach ($dependendComponents as $dComp) {
$dependencyModel = new Dependency();
$dependencyModel->setAttributes([
'count' => $dComp['quantity'],
'component_id' => $model->id,
'dependent_id' => $dComp['id']
]);
$dependencyModel->save();
}
return $this->redirect(['index', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model, 'tractorModels' => $tractorModels,
'components' => $components, 'depModels' => $depModels,
]);
}
}
这是我在" _form"
中的删除行jQUerywrapper.on("click", ".remove_field", function (e) {
var wantedDiv = $(this).parent('div').children().first();
var selectTag = $(wantedDiv).find('select');
var clickedId = $(selectTag).find('[selected=""]').attr('value');
var deletedIdsArray = $('#deletedIDs');
console.log($('#deletedIDs'));
if (clickedId) {
$('#deletedIds').append('<input type="hidden" name="deletedIds[]" value="' + clickedId + '">');
}
e.preventDefault();
$(this).parent('div').remove();
x--;
});
答案 0 :(得分:0)
您需要查看Yii中的内容:: $ app-&gt; request-&gt; post() 尝试var_dump(Yii :: $ app-&gt; request-&gt; post())。 如果没有任何内容,您需要更改输入表单。 http://www.yiiframework.com/doc-2.0/guide-input-forms.html
如果你想从数据库中删除一些内容,最好使用另一个Action:将数据从更新表单发送到actionDelet并根据需要更改方法逻辑。