fix: Remove deleted filter and primary key from insert

- Remove deleted=0 filter from existence check (allow soft-deleted updates)
- Remove primary key from insert payload to avoid conflicts
- Cleaner approach for upsert logic

Address CodeRabbit review feedback
This commit is contained in:
Ollama
2026-04-15 17:08:20 +00:00
parent a4c0d081a2
commit 196d1e4d3a

View File

@@ -450,10 +450,9 @@ class Item extends Model
// If id > 0 and record exists by primary key only, update it
if ($id > 0) {
// Check existence strictly by primary key
// Check existence strictly by primary key (regardless of soft-delete status)
$builder = $this->db->table('items');
$builder->where($primaryKey, $id);
$builder->where('deleted', 0);
$exists = $builder->countAllResults() > 0;
if ($exists) {
@@ -470,8 +469,12 @@ class Item extends Model
// Insert new record with transaction for atomicity
$this->db->transBegin();
// Remove primary key from insert payload if present
$insertData = $data;
unset($insertData[$primaryKey]);
$builder = $this->db->table('items');
$success = $builder->insert($data);
$success = $builder->insert($insertData);
if ($success) {
$data[$primaryKey] = (int)$this->db->insertID();