/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Presentacion::find();
     // add conditions that should always apply here
     $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;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'insumo_id' => $this->insumo_id, 'presentacion' => $this->presentacion, 'presentacionunidad_id' => $this->presentacionunidad_id, 'equivalencia' => $this->equivalencia, 'equivalenciasunidad_id' => $this->equivalenciasunidad_id]);
     $query->andFilterWhere(['like', 'insumo', $this->insumo])->andFilterWhere(['like', 'marca', $this->marca]);
     return $dataProvider;
 }
 /**
  * Finds the Presentacion model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Presentacion the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Presentacion::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemple #3
0
 /**
  * Updates an existing Insumo 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);
     $modelsPresentacion = $model->presentacions;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $oldIDs = ArrayHelper::map($modelsPresentacion, 'id', 'id');
         $modelsPresentacion = Model::createMultiple(Presentacion::className(), $modelsPresentacion);
         Model::loadMultiple($modelsPresentacion, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPresentacion, 'id', 'id')));
         // validate all models
         $valid = $model->validate();
         $valid = Model::validateMultiple($modelsPresentacion) && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         Presentacion::deleteAll(['id' => $deletedIDs]);
                     }
                     foreach ($modelsPresentacion as $modelPresentacion) {
                         $modelPresentacion->insumo_id = $model->id;
                         if (!($flag = $modelPresentacion->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['index']);
                     //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, 'modelsPresentacion' => empty($modelsPresentacion) ? [new Presentacion()] : $modelsPresentacion]);
     }
     return $this->redirect(['index']);
 }