/** * 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, 'quanitity' => $this->quanitity, 'po_id' => $this->po_id]); $query->andFilterWhere(['like', 'po_item_no', $this->po_item_no]); return $dataProvider; }
/** * 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); //$modelsPoitems = $model->poitems; oh that error !!! $modelsPoitems = Poitem::find()->where(['po_id' => $id])->all(); //it ture! :D if ($model->load(Yii::$app->request->post()) && $model->save()) { $oldIDs = ArrayHelper::map($modelsPoitems, 'id', 'id'); $modelsPoitems = Model::createMultiple(Poitem::classname(), $modelsPoitems); Model::loadMultiple($modelsPoitems, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPoitems, 'id', 'id'))); // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoitems) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { if (!empty($deletedIDs)) { Poitem::deleteAll(['id' => $deletedIDs]); } foreach ($modelsPoitems 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('update', ['model' => $model, 'modelsPoitems' => empty($modelsPoitems) ? [new Poitem()] : $modelsPoitems]); } }
/** * Finds the Poitem model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Poitem the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Poitem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }