Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`hrmates_db`.`leave_request`, CONSTRAINT `leave_request_ibfk_2` FOREIGN KEY (`reporting_manager_id`) REFERENCES `employee_details` (`employee_id`))
INSERT INTO `leave_request` (`employee_id`, `number_of_days`, `start_date`, `end_date`, `requested_date`, `reason`, `status`, `request_id`, `reporting_manager_id`) VALUES ('Fahad', '183', '2018-10-15', '2019-04-15', '2018-09-13 01:41:02', 'Casual Leave', 'submitted', 'KPRR651398', '')
Filename: C:/wamp/www/HRMATES/application/models/Database_models.php
Line Number: 389
Моя модель:
public function insert_leave_request($insert_data)
{
$output[TAG_QUERY_RESPONSE] = QUERY_FAILURE;
$insert_data[TAG_REQUEST_ID] = $this->Utility_models->get_id('leave_request',TAG_REQUEST_ID);
$this->db->select('*');
$qr =$this->db->get_where('employee_details',array('employee_id'=>$insert_data[TAG_EMPLOYEE_ID]))->row();
$insert_data[TAG_REPORTING_MANAGER_ID]= $qr->reporting_manager_id;
$output[TAG_HR_MANAGER_ID]= $qr->hr_manager_id;
$output[TAG_REPORTING_MANAGER_ID]= $qr->reporting_manager_id;
$output[TAG_REQUEST_ID]= $insert_data[TAG_REQUEST_ID];
$result = $this->db->insert('leave_request',$insert_data);
if($result)
{
$output[TAG_QUERY_RESPONSE] = QUERY_SUCCESS;
}
return $output;
}
Контроллер
function insert_leave_request()
{
$data[TAG_STRING] = '';
$insert_data[TAG_EMPLOYEE_ID] = $this->input->post(TAG_EMPLOYEE_ID);
$insert_data[TAG_NUMBER_OF_DAYS] = $this->input->post(TAG_NUMBER_OF_DAYS);
$insert_data[TAG_START_DATE] = $this->input->post(TAG_START_DATE);
$insert_data[TAG_END_DATE] = $this->input->post(TAG_END_DATE);
$insert_data[TAG_REQUESTED_DATE] = date('Y-m-d h:i:s');
$insert_data[TAG_REASON] = $this->input->post(TAG_REASON);
$insert_data[TAG_STATUS] = TAG_SUBMITTED;
$result_insert_leave=$this->Database_models->insert_leave_request($insert_data);
if($result_insert_leave){
$message_data[TAG_EMPLOYEE_ID] = $this->input->post(TAG_EMPLOYEE_ID);
$message_data[TAG_DATE] = date('Y-m-d h:i:s');
$message_data[TAG_MESSAGE] = STR_LEAVE_REQUEST;
$message_data[TAG_TO_ID] = $result_insert_leave[TAG_REPORTING_MANAGER_ID];
$message_data[TAG_TABLE_REFERENCE] =TAG_LEAVE_REQUEST;
$message_data[TAG_STATUS] =1;
$message_data[TAG_REFERENCE_ID] =$result_insert_leave[TAG_REQUEST_ID];
$result_insert_message1=$this->Database_models->insert_meassage($message_data);
$message_data[TAG_TO_ID] = $result_insert_leave[TAG_HR_MANAGER_ID];
if($result_insert_message1) {
$result_insert_message2 = $this->Database_models->insert_meassage($message_data);
if ($result_insert_message2) {
redirect(base_url() . 'leave_request/1');
}
}
}
redirect(base_url() . 'leave_request');
}