注意(8):未定义的索引:cakephp

时间:2015-05-06 07:34:08

标签: php cakephp cakephp-3.0

 - I have facing problem with notices and warning, please help me to
       resolved 

       Here is error:

       >Notice (8): Undefined index: energy_improvements_id
       >[APP/Controller/CustomerEvaluationsController.php, line 17] 

       Here is controller code:

           public function generate() {
               $this->loadModel('User');  
               $user=$this->User->save($this->request->data);

               $this->loadModel('UserUtilities');  
               $this->request->data['UserUtilities']['user_id']=$this->User->id;

               $this->UserUtilities->save($this->request->data);

               $data['energy_improvements_id']=$this->request->data['energy_improvements_id'];

               foreach ($data['energy_improvements_id'] as $key => $value) {
                   $temp[$key]['energy_improvements_id']=$value;
                   $temp[$key]['user_id']=$this->User->id;   
               }

               $this->set('user_id',$this->User->id);
                $this->loadModel('EnergyImprovementUser');
             $this->EnergyImprovementUser->deleteAll(array('user_id' => $this-   >User->id));

//这里是第41行临时代码解决通知(8):未定义的索引:cakephp //注意(8):未定义的变量:temp

// [APP / Controller / CustomerEvaluationsController.php,第41行]

  $this->EnergyImprovementUser->saveAll($temp);
       $this->layout='';
        //load the Electricity Utilities
        $this->loadModel('ElectricUtility');
        $electric_utilities = $this->ElectricUtility->find('list',      array('fields' => array('ElectricUtility.id', 'ElectricUtility.electric_utility')));
        $this->set(compact('electric_utilities'));

3 个答案:

答案 0 :(得分:1)

你必须定义id或创建它是否是新的:

$this->User->create();

 $this->UserUtilities->create();

尝试调试($ this-> request->数据)后查看您的表单是否发送了Id(energy_improvements_id)

答案 1 :(得分:0)

在foreach之前检查是否为空

if (!empty($this->request->data['energy_improvements_id'])){

    foreach ($this->request->data['energy_improvements_id'] as $key => $value) {
        $temp[$key]['energy_improvements_id']=$value;
        $temp[$key]['user_id']=$this->User->id;   
    }

       $this->EnergyImprovementUser->saveAll($temp);
       $this->layout='';
       //load the Electricity Utilities
       $this->loadModel('ElectricUtility');
       $electric_utilities = $this->ElectricUtility->find('list',      array('fields' => array('ElectricUtility.id', 'ElectricUtility.electric_utility')));
       $this->set(compact('electric_utilities'));

}

答案 2 :(得分:0)

php_value max_input_vars 10000 php_value suhosin.post.max_vars 10000 php_value suhosin.request.max_vars 10000

重置.htaccess(OR)php.ini文件中的值。