/** * Updates an existing Po model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $countPoItems = PoItem::find()->where(['po_id' => $id])->count(); $modelsPoItem = PoItem::find()->where(['po_id' => $id])->all(); if ($model->load(Yii::$app->request->post())) { // $model->save(); $oldIDs = ArrayHelper::map($modelsPoItem, 'id', 'id'); $modelsPoItem = Model::createMultiple(PoItem::classname(), $modelsPoItem); Model::loadMultiple($modelsPoItem, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPoItem, 'id', 'id'))); // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoItem) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { if (!empty($deletedIDs)) { PoItem::deleteAll(['id' => $deletedIDs]); } foreach ($modelsPoItem as $modelPoItem) { $modelPoItem->po_id = $model->id; if (!($flag = $modelPoItem->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'modelsPoItem' => $modelsPoItem]); } }
public function actionCreate() { $model = new Po(); $modelsPoItem = [new PoItem()]; if ($model->load(Yii::$app->request->post()) && $model->save()) { $modelsPoItem = Model::createMultiple(PoItem::classname()); Model::loadMultiple($modelsPoItem, Yii::$app->request->post()); // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoItem) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { foreach ($modelsPoItem as $modelPoItem) { $modelPoItem->po_id = $model->id; if (!($flag = $modelPoItem->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } else { return $this->render('create', ['model' => $model, 'modelsPoItem' => empty($modelsPoItem) ? [new PoItem()] : $modelsPoItem]); } }
/** * Creates a new Po model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Po(); $modelsPoItem = [new PoItem()]; if ($model->load(Yii::$app->request->post()) && $model->save()) { $oldIDs = ArrayHelper::map($modelsPoItem, 'id', 'id'); $modelsPoItem = Model::createMultiple(PoItem::classname(), $modelsPoItem); echo PoItem::classname(); echo '<pre>'; print_r($modelsPoItem); print_r($oldIDs); echo '</pre>'; die; Model::loadMultiple($modelsPoItem, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPoItem, 'id', 'id'))); // ajax validation // if (Yii::$app->request->isAjax) { // Yii::$app->response->format = Response::FORMAT_JSON; // return ArrayHelper::merge( // ActiveForm::validateMultiple($modelsPoItem), // ActiveForm::validate($modelCustomer) // ); // } // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoItem) && $valid; // echo '<pre>'; // print_r($valid); // echo '</pre>'; // die(); if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { if (!empty($deletedIDs)) { PoItem::deleteAll(['id' => $deletedIDs]); } foreach ($modelsPoItem as $modelPoItem) { $modelPoItem->po_id = $model->id; if (!($flag = $modelPoItem->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } else { return $this->render('create', ['model' => $model, 'modelsPoItem' => empty($modelsPoItem) ? [new PoItem()] : $modelsPoItem]); } }
/** * Creates a new Po model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Po(); $modelsPoItem = [new PoItem()]; if ($model->load(Yii::$app->request->post())) { $modelsPoItem = Model::createMultiple(PoItem::classname()); Model::loadMultiple($modelsPoItem, Yii::$app->request->post()); // ajax validation /*if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge( ActiveForm::validateMultiple($modelsPoItem), ActiveForm::validate($model) ); }*/ // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoItem) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { foreach ($modelsPoItem as $modelPoItem) { $modelPoItem->po_id = $model->id; if (!($flag = $modelPoItem->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } // return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'modelsPoItem' => empty($modelsPoItem) ? [new PoItem()] : $modelsPoItem]); } }