/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ProveedoresContacto::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(['pcontacto_id' => $this->pcontacto_id, 'proveedorid' => $this->proveedorid]);
     $query->andFilterWhere(['like', 'nombre', $this->nombre])->andFilterWhere(['like', 'apellido', $this->apellido])->andFilterWhere(['like', 'telefono', $this->telefono])->andFilterWhere(['like', 'celular', $this->celular])->andFilterWhere(['like', 'email', $this->email]);
     return $dataProvider;
 }
示例#2
0
 public function beforeDelete()
 {
     ProveedoresContacto::deleteAll('proveedorid =' . $this->proveedor_id);
     return parent::beforeDelete();
     // TODO: Change the autogenerated stub
 }
 /**
  * Updates an existing Proveedores 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);
     $modelsContactos = $model->proveedoresContactos;
     if ($model->load(Yii::$app->request->post())) {
         // Lista todos los contactos
         $oldIDs = ArrayHelper::map($modelsContactos, 'pcontacto_id', 'pcontacto_id');
         $modelsContactos = Model::createMultiplepcontacto(ProveedoresContacto::classname(), $modelsContactos);
         Model::loadMultiple($modelsContactos, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsContactos, 'pcontacto_id', 'pcontacto_id')));
         // validate all models
         $valid = $model->validate();
         // Debido a que el campo dependenciaid no se captura ( y es un campo obligatorio)
         $valid = Model::validateMultiple($modelsContactos, ['nombre', 'apellido', 'telefono', 'celular', 'email']) && $valid;
         // Modificando los contactos
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         ProveedoresContacto::deleteAll(['pcontacto_id' => $deletedIDs]);
                     }
                     foreach ($modelsContactos as $modelContacto) {
                         $modelContacto->proveedorid = $model->proveedor_id;
                         if (!($flag = $modelContacto->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['index']);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     } else {
         return $this->render('update', ['model' => $model, 'modelsContactos' => empty($modelsContactos) ? [new ProveedoresContacto()] : $modelsContactos]);
     }
 }
 /**
  * Finds the ProveedoresContacto model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ProveedoresContacto the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ProveedoresContacto::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }