fixing contactdetail empty not delete
This commit is contained in:
parent
501ef06592
commit
e5d51dff86
@ -103,13 +103,13 @@ class Contact extends Controller {
|
||||
if (!$ContactID) { throw new \RuntimeException('Failed to insert contact'); }
|
||||
}
|
||||
|
||||
if (!empty($input['Details'])) {
|
||||
$result = $detailModel->syncDetails($ContactID, $input['Details']);
|
||||
if ($result['status'] !== 'success') {
|
||||
throw new \RuntimeException('Failed to sync details: ' . $result['message']);
|
||||
}
|
||||
|
||||
$result = $detailModel->syncDetails($ContactID, $input['Details']);
|
||||
if ($result['status'] !== 'success') {
|
||||
throw new \RuntimeException('Failed to sync details: ' . $result['message']);
|
||||
}
|
||||
|
||||
|
||||
$db->transComplete();
|
||||
|
||||
if ($db->transStatus() === false) {
|
||||
|
||||
@ -11,48 +11,48 @@ class ContactDetailModel extends Model {
|
||||
|
||||
public function syncDetails(int $ContactID, array $contactDetails) {
|
||||
try {
|
||||
$keptSiteIDs = [];
|
||||
$keptSiteIDs = [];
|
||||
|
||||
foreach ($contactDetails as $detail) {
|
||||
if (empty($detail['SiteID'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$detail['ContactID'] = $ContactID;
|
||||
|
||||
$existing = $this->where('ContactID', $ContactID)
|
||||
->where('SiteID', $detail['SiteID'])
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
$this->update($existing[$this->primaryKey], $detail);
|
||||
} else {
|
||||
$this->insert($detail);
|
||||
}
|
||||
|
||||
$keptSiteIDs[] = $detail['SiteID'];
|
||||
foreach ($contactDetails as $detail) {
|
||||
if (empty($detail['SiteID'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Delete missing rows
|
||||
if (!empty($keptSiteIDs)) {
|
||||
$this->where('ContactID', $ContactID)
|
||||
->whereNotIn('SiteID', $keptSiteIDs)
|
||||
->delete();
|
||||
$detail['ContactID'] = $ContactID;
|
||||
|
||||
$existing = $this->where('ContactID', $ContactID)
|
||||
->where('SiteID', $detail['SiteID'])
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
$this->update($existing[$this->primaryKey], $detail);
|
||||
} else {
|
||||
$this->where('ContactID', $ContactID)->delete();
|
||||
$this->insert($detail);
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'inserted' => count($contactDetails) - count($keptSiteIDs),
|
||||
'kept' => count($keptSiteIDs),
|
||||
];
|
||||
$keptSiteIDs[] = $detail['SiteID'];
|
||||
}
|
||||
|
||||
// Delete missing rows
|
||||
if (!empty($keptSiteIDs)) {
|
||||
$this->where('ContactID', $ContactID)
|
||||
->whereNotIn('SiteID', $keptSiteIDs)
|
||||
->delete();
|
||||
} else {
|
||||
$this->where('ContactID', $ContactID)->delete();
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'inserted' => count($contactDetails) - count($keptSiteIDs),
|
||||
'kept' => count($keptSiteIDs),
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'syncDetails error: ' . $e->getMessage());
|
||||
|
||||
return [
|
||||
'status' => 'error',
|
||||
'message' => $e->getMessage(),
|
||||
'status' => 'error',
|
||||
'message' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user