/** * Creates a new ProyectoAeMeta model. * For ajax request will return json object * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page. * @param int $accionEspecifica ID de la accion especifica * @return mixed */ public function actionCreate($accionEspecifica) { $request = Yii::$app->request; $model = new ProyectoAeMeta(); $model->id_proyecto_accion_especifica = $accionEspecifica; if ($request->isAjax) { /* * Process for ajax request */ Yii::$app->response->format = Response::FORMAT_JSON; if ($request->isGet) { return ['title' => "Crear Meta", 'content' => $this->renderAjax('create', ['model' => $model]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])]; } else { if ($model->load($request->post()) && $model->save()) { return ['forceReload' => '#especifica-pjax', 'title' => "Create new ProyectoAeMeta", 'content' => '<span class="text-success">Create ProyectoAeMeta success</span>', 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])]; } else { return ['title' => "Crear Meta", 'content' => $this->renderAjax('create', ['model' => $model]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])]; } } } else { /* * Process for non-ajax request */ if ($model->load($request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } } }
/** * Override afterSave. * @param boolean $insert Verificar si se inserta o actualiza. * @param array $changedAttributes Atributos que se van a guardar. * @return afterSave(). */ public function afterSave($insert, $changedAttributes) { if (!$insert) { //nada } else { //crear modelo relacionado $meta = new ProyectoAeMeta(); //Llave foranea $meta->id_proyecto_accion_especifica = $this->id; //Colocar meses en cero $meta->enero = 0; $meta->febrero = 0; $meta->marzo = 0; $meta->abril = 0; $meta->mayo = 0; $meta->junio = 0; $meta->julio = 0; $meta->agosto = 0; $meta->septiembre = 0; $meta->octubre = 0; $meta->noviembre = 0; $meta->diciembre = 0; $meta->estatus = 1; //activo $meta->fecha_creacion = date('Y-m-d H:i:s'); $meta->save(false); } return parent::afterSave($insert, $changedAttributes); }