Search code examples
yii-extensionsyii

Yii/Giix - Controller Error when saving to related tables


When testing a CRUD generated Create form I receive the following error. Anyone familiar with this problem? Thanks ahead of time.

undefined index: contactindivs

07         $this->render('view', array(
08             'model' => $this->loadModel($id, 'Companylocation'),
09         ));
10     }
11 
12     public function actionCreate() {
13         $model = new Companylocation;
14 
15 
16         if (isset($_POST['Companylocation'])) {
17             $model->setAttributes($_POST['Companylocation']);
18             $relatedData = array(
19                 'contactindivs' => $_POST['Companylocation']['contactindivs'] === '' ? null : $_POST['Companylocation']['contactindivs'],
20                 );
21 
22             if ($model->saveWithRelated($relatedData)) {
23                 if (Yii::app()->getRequest()->getIsAjaxRequest())
24                     Yii::app()->end();
25                 else
26                     $this->redirect(array('view', 'id' => $model->CompanyLocationID));
27             }
28         }
29 
30         $this->render('create', array( 'model' => $model));
31     }

Solution

  • Line 19 is stating that if contactindivs === '' make it null. change it to this and it shouldn't give the error.

    $contactindivs = isset($_POST['Companylocation']['contactindivs']) ? $_POST['Companylocation']['contactindivs'] : '';
    $relatedData = array(
         'contactindivs' => $contactindivs,);
    

    The problem is that your getting a warning because you check for 'Companylocation', but not for ['Companylocation']['contactindivs']. That should fix it.