无法将数据保存到数据库cakephp

时间:2017-05-16 03:47:45

标签: php mysql cakephp

我的表SubjectTutor的列idsubject_idtutor_idprovince_idcity_idschool_id,{{1} }和state_private。我想将数据插入SubjectTutor表,但错误是

  

statusprovince_idcity_idschool_id不会   保存到表SubjectTutor中,但state_privateidsubject_id   将被保存。

这是控制器。我还设置了tutor_idprovince_idcity_idschool_id值。

state_private

这是ctp文件:

public function admin_add_pack($standard_id=null) 
{
    $this->loadModel('Plan');
    $this->loadModel('SubjectTutor');
    $this->loadModel('Standard');
    $this->loadModel('Province');
    $this->loadModel('City');
    $this->loadModel('School');
    $this->loadModel('User');

    $standard_data = $this->Standard->find('first',array('conditions'=>array('Standard.id'=>$standard_id)));

    $province_id = 32;
    $city_id = 44;
    $school_id = 1855;
    $this->set('title_for_layout','Subject');   

    if(!empty($this->request->data))
    {           
        if (!isset($this->request->params['_Token']['key']) || ($this->request->params['_Token']['key'] != $this->request->params['_Token']['key'])) 
        {
            $blackHoleCallback = $this->Security->blackHoleCallback;
            $this->$blackHoleCallback();
        }

        //validate user data
        $this->SubjectTutor->set($this->request->data['SubjectTutor']);
        $this->SubjectTutor->setValidation('add');

        if ($this->SubjectTutor->validates()) 
        {
            // $this->request->data['Subject']['user_id'] = $this->Auth->user('id');
            $userdata   =   $this->request->data['SubjectTutor'];
            $this->SubjectTutor->save($userdata,false);
            $subject_tutor_id   =   $this->SubjectTutor->id;

            $this->SubjectTutor->school_id = 1855;
            $this->SubjectTutor->province_id = 32;
            $this->SubjectTutor->city_id = 44;
            $this->SubjectTutor->state_private = 1;
            $this->Session->setFlash("Record has been added successfully", 'admin_flash_good');
            $this->redirect(array('controller'=>'standards', 'action'=>'subject_list',$standard_id));
        } 
        else 
        {   
            $this->Session->setFlash("Record has not been created", 'admin_flash_bad');
        }
    }
    $tutors =   $this->User->getStandardTutorList($standard_id);
    $subjects = $this->Subject->getStandardSubjectList($standard_id);
    $standards = $this->Standard->getStandardList();
    $provinces = $this->Province->getProvinceList();
    $cities = $this->City->getCityList();
    $schools = $this->School->getSchoolList();

    $this->set(compact('provinces'));
    $this->set(compact('standards','standard_id','province_id','city_id','school_id','standard_data','tutors','subjects','provinces','cities','schools','tutor_id'));
}

1 个答案:

答案 0 :(得分:0)

当您对视图中的字段使用“禁用”选项时,在保存表单数据时不会设置该字段。要么使用“readonly”选项,要么在您之前在控制器中设置这些值保存(因为您无论如何都不需要用户输入)。或者将值显示给用户而不假装他们有任何输入,并使用隐藏字段传递正确的数据。