yii2-dynamicform
yii2-dynamicform copied to clipboard
When updating, all values in related table are deleted and inserted again
Hello: I have two models: Po and PoItem, one Po can have multiple PoItems, which I manage to create with dynamic form, all is working, creating and updating.
But when I update something strange happens, if I modify anything in Po or PoItem (is the same form), everything (regarding the Po I'm updateing) in the PoItem table is deleted and inserted again with new id's.
So far this has not been an issue, because the newly created PoItems mantain their values or get updated in case of something changes on them.
Question is: is this normal behavior? I find strange that even if the PoItems are not modified on update anyways get re-inserted.
This is my actionUpdate code: `public function actionUpdate($id) { $model = $this->findModel($id); $modelsItemOc = $model->itemOcs;
if ($model->load(Yii::$app->request->post())) {
$oldIDs = ArrayHelper::map($modelsItemOc, 'id_item_oc', 'id_item_oc');
$modelsItemOc = Model::createMultiple(ItemOc::classname(), $modelsItemOc);
Model::loadMultiple($modelsItemOc, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsItemOc, 'id_item_oc', 'id_item_oc')));
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsItemOc),
ActiveForm::validate($model)
);
}
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsItemOc) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
ItemOc::deleteAll(['id_item_oc' => $deletedIDs]);
}
foreach ($modelsItemOc as $modelItemOc) {
$modelItemOc->oc_id = $model->id_orden_de_compra;
if (! ($flag = $modelItemOc->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id_orden_de_compra]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('update', [
'model' => $model,
'modelsItemOc' => (empty($modelsItemOc)) ? [new ItemOc] : $modelsItemOc
]);
}`
function _getWidgetOptionsRoot()
var _updateAttributes = function(widgetOptions) { // $(widgetOptionsRoot.widgetItem).each(function(index) { $('.' + widgetOptions.widgetContainer).find(widgetOptions.widgetItem).each(function(index) { ...... } }
Not too sure about your problem, you can try the above method.
Hi, it possible to risolve this update problem?
any one can suggest a solution? I got the same issue, since I got another relation table with the "PoItem" like above, the "delete-insert" action will remove my relation record.
I was try to solve this problem. And I have an answer to solve it (not sure).
I was perform the softDelete
.
Before you try the code below, make sure your table has column name deleted
. So if the value is 0
, then it is not "deleted" and vice versa.
Copy this code inside your actionUpdate
:
if (! empty($deletedIDs)) {
//ItemOc::deleteAll(['id_item_oc' => $deletedIDs]);
$modelDelete = ItemOc::find()->where(['id_item_oc' => $deletedIDs[$model->id_item_oc]])->all();
foreach($modelDelete as $del){
$del->deleted = 1; //Performing the softDelete, change the value into 1
$del->save();
}
}
The problem is: how can you view the row that have deleted
value that is 0
in your view
?