İlk temizlik tamamlandı bir önceki projeden
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?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
|
||||
|
||||
} ?>
|
||||
Reference in New Issue
Block a user