public function actionAjaxPersona()
 {
     $model = new Persona();
     if (Yii::app()->request->isAjaxRequest) {
         $post = trim(file_get_contents('php://input'));
         //por ejemplo traeria: "cedula=123&nombre=aasas&apellido=aaa"
         // como lo sabemos ? simple: Yii::log("POST=".$post,"info");
         // ahora los pasamos a un array con forma key=>value
         // para que model->attributes los acepte:
         $attributes = array();
         foreach (explode("&", $post) as $item) {
             $att = explode("=", $item);
             $attributes[$att[0]] = $att[1];
         }
         // listo hemos convertido el string post a un array indexado:
         // var_export($attributes,true) mostraria:
         //  array ( 'cedula' => '123', 'nombre' => 'aasas', 'apellido' => 'aaa', )
         $model->attributes = $attributes;
         if ($model->validate()) {
             // ok todo bien, haces algo aqui con el modelo...
             // como es un ejemplo no haremos nada mas que informar.
             return;
         } else {
             // si defined('YII_DEBUG') or define('YII_DEBUG',true);
             // es TRUE por defecto, ver /index.php
             // entonces la excepcion mostrara un codigo horrible,
             // pero si la ponemos en FALSE, entonces solo mostrara
             // el errorSummary, lo cual es deseable.
             throw new Exception(CHtml::errorSummary($model));
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     /**	$model=new FichaMedica;
     
     		// Uncomment the following line if AJAX validation is needed
     		// $this->performAjaxValidation($model);
     
     		if(isset($_POST['FichaMedica']))
     		{
     			$model->attributes=$_POST['FichaMedica'];
     			if($model->save())
     				$this->redirect(array('view','id'=>$model->idFicha_Medica));
     		}
     
     		$this->render('create',array(
     			'model'=>$model,
     		));*/
     $model1 = new FichaMedica();
     $model2 = new Persona();
     if (isset($_POST['FichaMedica'])) {
         // populate input data to $a and $b
         $model1->attributes = $_POST['FichaMedica'];
         $model2->attributes = $_POST['Persona'];
         $existe = Persona::model()->findByAttributes(array('Cedula' => $model2->Cedula));
         if ($existe == null) {
             // validate BOTH $a and $b
             $valid = $model1->validate();
             $valid = $model2->validate() && $valid;
             if ($valid) {
                 $model2->save(false);
                 $model1->idPariente = $model2->idPersona;
                 $model1->save();
             }
         } else {
             $model2 = $existe;
             $valid = $model1->validate();
             $valid = $model2->validate() && $valid;
             if ($valid) {
                 $model1->idPariente = $model2->idPersona;
                 $model1->save();
             }
         }
     }
     if (isset($_POST['FichaMedica'])) {
         $model1->attributes = $_POST['FichaMedica'];
         if ($model1->save()) {
             $this->redirect(array('view', 'id' => $model1->idFicha_Medica));
         }
     }
     $this->render('create', array('model2' => $model2, 'model1' => $model1));
 }