/** * 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]); } }
/** * 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; }