예제 #1
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $model->scenario = 'update';
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Families'])) {
         $model->attributes = $_POST['Families'];
         $models = array($model);
         $parents = array();
         foreach (array('husband', 'wife') as $person) {
             if (isset($_POST['People'][$person])) {
                 $p = new People();
                 #						$pid = $_POST['People'][$person]['id'];
                 if ($pid = $_POST['People'][$person]['id']) {
                     $p = People::model()->findByPk($pid);
                 }
                 $p->attributes = $_POST['People'][$person];
                 $p->role = $person;
                 $parents[$person] = $p;
                 array_push($models, $p);
             }
         }
         $dependents = array();
         if (isset($_POST['People']['dependent'])) {
             for ($i = 0; $i < 2; ++$i) {
                 if (isset($_POST['People']['dependent'][$i])) {
                     $p = new People();
                     if ($pid = $_POST['People']['dependent'][$i]['id']) {
                         $p = People::model()->findByPk($pid);
                     }
                     $p->attributes = $_POST['People']['dependent'][$i];
                     $p->role = 'dependent';
                     $dependents[$i] = $p;
                 }
             }
         }
         $children = array();
         if (isset($_POST['People']['child'])) {
             for ($i = 0; $i < 4; ++$i) {
                 if (isset($_POST['People']['child'][$i])) {
                     $p = new People();
                     if ($pid = $_POST['People']['child'][$i]['id']) {
                         $p = People::model()->findByPk($pid);
                     }
                     $p->attributes = $_POST['People']['child'][$i];
                     $p->role = 'child';
                     $children[$i] = $p;
                 }
             }
         }
         $models = array_merge($models, $dependents);
         $models = array_merge($models, $children);
         $this->performAjaxValidation($models);
         if ($model->save()) {
             foreach ($parents as $parent) {
                 $parent->family_id = $model->id;
                 $parent->save();
                 switch ($parent->role) {
                     case 'husband':
                         if (!isset($model->husband_id)) {
                             $model->saveAttributes(array('husband_id' => $parent->id));
                         }
                         break;
                     case 'wife':
                         if (!isset($model->wife_id)) {
                             $model->saveAttributes(array('wife_id' => $parent->id));
                         }
                         break;
                 }
             }
             foreach ($dependents as $dependent) {
                 $dependent->family_id = $model->id;
                 $dependent->save();
             }
             foreach ($children as $child) {
                 $child->family_id = $model->id;
                 $child->save();
             }
             $model = $this->loadModel($id);
         }
     }
     $ppl_ac = People::getAutoCompleteFields();
     $fam_ac = Families::getAutoCompleteFields();
     $fam_ac['churches'] = $ppl_ac['churches'];
     $this->render('update', array('model' => $model, 'ppl_ac' => $ppl_ac, 'fam_ac' => $fam_ac));
 }