Can not save data to Database cakephp


#1

I have table SubjectTutor who has column id, subject_id, tutor_id, province_id, city_id, school_id, state_private and status. I want to insert data into SubjectTutor table but the error:

province_id, city_id, school_id and state_private will not saved into table SubjectTutor, but id, subject_id,and tutor_id will be saved.

this is controller.php. i have try but did not saved, please help :sob:

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->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'));
	}

this is ctp file:

<div class="form-group">
<div class="col-sm-6">
    <label for="exampleInputPassword1"> Select Tutor </label>
        <?php echo($this->Form->input('SubjectTutor.tutor_id', array('options'=>$tutors,'div'=>false, 'label'=>false, "class" => "form-control",'empty'=>'Select Tutor')));?>
</div>
</div>

<div class="form-group">
<div class="col-sm-6">
    <label for="exampleInputPassword1"> Select Standard </label>
        <?php echo ($this->Form->input('Subject.standard_id2', array('options'=>$standards,"div"=>false,"default"=>$standard_id,"label"=>false,"class"=>"form-control",'disabled'=>true))); ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
    <label for="exampleInputPassword1">Select Subject </label>
        <?php echo ($this->Form->input("SubjectTutor.subject_id", array('empty'=>'--Select Subject--', 'options'=>$subjects,"div"=>false,"label"=>false,"class"=>"form-control",'disabled'=>false))); ?>
</div>
</div>      
<div class="form-group">
<div class="col-sm-6">
    <label for="exampleInputPassword1">Province </label>
        <?php echo ($this->Form->input("SubjectTutor.province_id", array('empty'=>'--Select Province--', 'options'=>$provinces,"div"=>false,"default"=>$province_id,"label"=>false,"class"=>"form-control",'disabled'=>true))); ?>
</div>
</div>          
<div class="form-group">
<div class="col-sm-6">
     <label for="exampleInputPassword1">City </label>
            <?php echo ($this->Form->input("SubjectTutor.city_id", array('empty'=>'--Select City--', 'options'=>$cities,"div"=>false,"default"=>$city_id,"label"=>false,"class"=>"form-control",'disabled'=>true))); ?>
</div>
</div>  
<div class="form-group">
<div class="col-sm-6">
    <label for="exampleInputPassword1">School </label>
        <?php echo ($this->Form->input("SubjectTutor.school_id", array('empty'=>'--Select School--', 'options'=>$schools,"div"=>false,"default"=>$school_id,"label"=>false,"class"=>"form-control",'disabled'=>true))); ?>
</div>
</div>

#2

If you can, debug the $userdata and see if the values are correct.
You don’t tell when error happens, i suppose on save, after validation. Is cakephp 2.x?


#3

Yes, i am using cakephp 2.x.
I can not debug $userdata.
The error happens while i try to save data into database. Some data (id, tutor_id, and subject_id) will be saved into SubjectTutor table, but some data (province_id, city_id, school_id) will not saved.


#4

Maybe, it happens because the input are disabled, i think in this case, there are not sended in the request.
Check inputs, province_id, city_id, school_id are disabled