2025-09-24 16:15:55 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
|
|
class ContactDetailModel extends Model {
|
|
|
|
|
protected $table = 'contactdetail';
|
|
|
|
|
protected $primaryKey = 'ContactDetID';
|
|
|
|
|
protected $allowedFields = ['ContactID', 'SiteID', 'ContactCode', 'ContactEmail', 'OccupationID', 'JobTitle', 'Department', 'ContactStartDate', 'ContactEndDate'];
|
2025-09-25 09:42:17 +07:00
|
|
|
|
|
|
|
|
public function syncDetails(int $contactId, array $details) {
|
|
|
|
|
$kept = [];
|
|
|
|
|
|
|
|
|
|
foreach ($details as $detail) {
|
|
|
|
|
$detail['ContactID'] = $contactId;
|
|
|
|
|
|
|
|
|
|
$existing = $this->where('SiteID', $detail['SiteID'])
|
|
|
|
|
->where('ContactID', $contactId)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($existing) {
|
|
|
|
|
$this->update($existing[$this->primaryKey], $detail);
|
|
|
|
|
$kept[] = $existing[$this->primaryKey];
|
|
|
|
|
} else {
|
|
|
|
|
$newId = $this->insert($detail);
|
|
|
|
|
$kept[] = $newId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($kept)) {
|
|
|
|
|
$this->where('ContactID', $contactId)
|
|
|
|
|
->whereNotIn($this->primaryKey, $kept)
|
|
|
|
|
->delete();
|
|
|
|
|
} else { $this->where('ContactID', $contactId)->delete(); }
|
|
|
|
|
}
|
2025-09-24 16:15:55 +07:00
|
|
|
}
|