Check the below code I have converted this from web code , almost similar code is working on web now when I converted it into api to integrate it into app, I am facing issue: “error”: “SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘location_id’ cannot be null” Even tought I have set the Column ‘location_id’ to null and set it’s default value to 0 And most weird thing is that it’s creating an entry is same table as I am passing by default 1 to location_id and it shows in entry as well then how can I receive this constraint error Please help me to resolve this issue
public function beneficiaryAddApi() {
$this->autoRender = false;
$this->response->type('json');
$this->loadModel('Person');
$this->Auth->userModel = 'Person';
$this->Person->useDbConfig = 'defaultHospital';
// Check if the form data has been submitted
if ($this->request->is('post')) {
try {
$email = $this->request->data['email'];
$mobile = $this->request->data['mobile'];
// Check if a user with the same email and mobile already exists
$existingPerson = $this->Person->find('first', [
'conditions' => [
'Person.email' => $email,
'Person.mobile' => $mobile
]
]);
if (!empty($existingPerson)) {
// User already exists
$response = ['success' => false, 'message' => 'User with this email and mobile number already exists.'];
return $this->response->body(json_encode($response));
}
// Generate the auto-generated patient ID
$uid = $this->autoGeneratedPatientIDonline();
// Save the form data into the Person model
$this->Person->create();
$this->request->data['Person']['patient_uid'] = $uid; // Assign the generated ID to the Person's patient_id field
$this->request->data['Person']['first_name'] = $this->request->data['first_name'];
$this->request->data['Person']['last_name'] = $this->request->data['last_name'];
$this->request->data['Person']['age'] = $this->request->data['age'];
$this->request->data['Person']['sex'] = $this->request->data['sex'];
$this->request->data['Person']['dob'] = $this->request->data['dob'];
$this->request->data['Person']['mobile'] = $this->request->data['mobile'];
$this->request->data['Person']['password'] = $this->request->data['password'];
$this->request->data['Person']['admission_type'] = $this->request->data['admission_type'];
$this->request->data['Person']['from'] = $this->request->data['from'];
$this->request->data['Person']['email'] = $this->request->data['email'];
$this->request->data['Person']['location_id'] = NULL;
// print_r('sdbjskfnsk');die;
$issave = $this->Person->save($this->request->data);
$personid = $this->Person->id;
if ($issave) {
further work
} else {
// Error saving data into the Person model
$response = ['success' => false, 'message' => 'Unable to save the person.'];
CakeLog::write('error', 'Unable to save Person: ' . json_encode($this->Person->validationErrors));
return $this->response->body(json_encode($response));
}
} catch (Exception $e) {
// Log any exceptions that occur
CakeLog::write('error', 'Exception: ' . $e->getMessage());
$response = ['success' => false, 'message' => 'Internal Server Error', 'error' => $e->getMessage()];
return $this->response->body(json_encode($response));
}
}
}