/** * 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]); } }
protected function findModel($id) { if (($model = PoItem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 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 data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = PoItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); /*if ($this->load($params); && !$this->validate()) { return $dataProvider; }*/ $query->andFilterWhere(['id' => $this->id, 'quantity' => $this->quantity, 'po_id' => $this->po_id]); $query->andFilterWhere(['like', 'po_item_no', $this->po_item_no]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = PoItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'quantity' => $this->quantity, 'po_id' => $this->po_id]); $query->andFilterWhere(['like', 'po_item_no', $this->po_item_no]); return $dataProvider; }
/** * 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]); } }
/** * @return \yii\db\ActiveQuery */ public function getPoItems() { return $this->hasMany(PoItem::className(), ['po_id' => 'id']); }