36 lines
1000 B
PHP
36 lines
1000 B
PHP
<?php
|
|
/**
|
|
* Try catch kullanarak ekler veya günceller
|
|
* Transaction management added to prevent lock issues
|
|
*/
|
|
function firstOrUpdate($data, $table, $where, $debug = false) {
|
|
|
|
if(isset($data['id'])) {
|
|
if($data['id'] == "") {
|
|
unset($data['id']);
|
|
}
|
|
}
|
|
|
|
return \DB::transaction(function() use ($data, $table, $where, $debug) {
|
|
try {
|
|
$data['created_at'] = simdi();
|
|
$id = db($table)->insertGetId($data);
|
|
return $id;
|
|
} catch (\Throwable $th) {
|
|
|
|
if($debug) {
|
|
dump($th);
|
|
}
|
|
|
|
// Use lockForUpdate to prevent race conditions
|
|
$affectedRows = db($table)
|
|
->where($where)
|
|
->lockForUpdate()
|
|
->get();
|
|
|
|
$affectedRows = db($table)->where($where)->update($data);
|
|
return "update $affectedRows";
|
|
}
|
|
}, 3); // 3 retry attempts
|
|
|
|
} ?>
|