select("c.ContactID, cd.SiteID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty") ->join("contactdetail cd", "c.ContactID=cd.ContactID", "left") ->join("occupation o","cd.OccupationID=o.OccupationID", "left") ->get()->getResultArray(); return $rows; } public function getContactWithDetail($ContactID) { $rows = $this->where('c.ContactID', $ContactID)->join('contactdetail cd', 'c.ContactID=cd.ContactID','left')->get()->getResultArray(); $contact = [ 'ContactID' => $rows[0]['ContactID'], 'NameFirst' => $rows[0]['NameFirst'] ?? null, 'NameLast' => $rows[0]['NameLast'] ?? null, 'Title' => $rows[0]['Title'] ?? null, 'Initial' => $rows[0]['Initial'] ?? null, 'Birthdate' => $rows[0]['Birthdate'] ?? null, 'EmailAddress1' => $rows[0]['EmailAddress1'] ?? null, 'EmailAddress2' => $rows[0]['EmailAddress2'] ?? null, 'Phone' => $rows[0]['Phone'] ?? null, 'MobilePhone1' => $rows[0]['MobilePhone1'] ?? null, 'MobilePhone2' => $rows[0]['MobilePhone2'] ?? null, 'Specialty' => $rows[0]['Specialty'] ?? null, 'SubSpecialty' => $rows[0]['SubSpecialty'] ?? null, 'Details' => [] ]; foreach ($rows as $row) { if (!empty($row['ContactDetID'])) { $contact['Details'][] = [ 'SiteID' => $row['SiteID'] ?? null, 'ContactDetID' => $row['ContactDetID'], 'ContactCode' => $row['ContactCode'] ?? null, 'ContactEmail' => $row['DetailPhone'] ?? null, 'OccupationID' => $row['OccupationID'] ?? null, 'JobTitle' => $row['JobTitle'] ?? null, 'Department' => $row['Department'] ?? null, 'ContactStartDate' => $row['ContactStartDate'] ?? null, 'ContactEndDate' => $row['ContactEndDate'] ?? null ]; } } return $contact; } public function createContact(array $contactData, array $contactDetails) { $db = \Config\Database::connect(); $db->transStart(); try { if (!$this->insert($contactData)) { throw new \Exception('Failed to insert contact'); } $ContactID = $this->getInsertID(); $detailModel = new \App\Models\ContactDetailModel(); foreach ($contactDetails as $detail) { $detail['ContactID'] = $ContactID; if (!$detailModel->insert($detail)) { throw new \Exception('Failed to insert contact details'); } } $db->transComplete(); return $db->transStatus(); } catch (\Exception $e) { $db->transRollback(); throw $e; } } public function updateContact(int $ContactID, array $contactData, array $contactDetails) { $db = \Config\Database::connect(); $db->transStart(); try { if (!$this->update($ContactID, $contactData)) { throw new \Exception('Failed to update contact'); } $detailModel = new \App\Models\ContactDetailModel(); $detailModel->syncDetails($ContactID, $contactDetails); $db->transComplete(); return $db->transStatus(); } catch (\Exception $e) { $db->transRollback(); throw $e; } } }