Skip to content

Instantly share code, notes, and snippets.

@milinmestry
Created September 20, 2019 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milinmestry/1bcd1b6c1270cca8ce9335c83c546a32 to your computer and use it in GitHub Desktop.
Save milinmestry/1bcd1b6c1270cca8ce9335c83c546a32 to your computer and use it in GitHub Desktop.
Eloquent save() method doesn't work and does not output any error/exception
<?php
// Model
namespace App;
class CloseDayBreakdownCall extends Model {
public $timestamps = false;
protected $fillable = [
'api_start_date', 'api_end_date', 'api_call_datetime',
'api_call_status', 'file_download_status', 'download_filename',
'file_download_datetime', 'store_in_db_status', 'store_in_db_datetime',
'api_filters', 'remarks', 'api_resp_last_update_time',
'api_resp_last_update_time_gmt', 'api_timezone'
];
public static function updateJsonToDBStatus($params = []) {
$id = $params['id'] ?? 0;
if ($id < 1) {
return -1;
}
$instanceCDB = self::find($id);
// $instanceCDB = self::where('id', $id)->first();
if ($instanceCDB) {
$remarks = $params['remarks'] ?? '';
$storeDbStatus = $params['storeDbStatus'] ?? null;
$lastUpdateTime = $params['apiRespLastUpdateTime'] ?? null;
$lastUpdateTimeGmt = $params['apiRespLastUpdateTimeGmt'] ?? null;
$apiTimezone = $params['apiTimezone'] ?? null;
$dbStatusInt = self::getIntForStatusText($storeDbStatus);
$instanceCDB->store_in_db_status = $dbStatusInt;
$instanceCDB->store_in_db_datetime = DateHandler::getUTCTimestamp();
if (!empty($lastUpdateTime)) {
$instanceCDB->api_resp_last_update_time = $lastUpdateTime;
$instanceCDB->api_resp_last_update_time_gmt = $lastUpdateTimeGmt;
$instanceCDB->api_timezone = $apiTimezone;
}
if (!empty($remarks)) {
$instanceCDB->remarks .= $remarks . PHP_EOL;
}
// \DB::connection()->enableQueryLog();
$instanceCDB->save();
// $queries = \DB::getQueryLog();
// dd($queries);
// $columnsChanged = $instanceCDB->getChanges();
// dd($columnsChanged);
// $instanceCDB->update(['remarks' => $remarks]);
return $instanceCDB->id;
}
return -1;
}
}
<?php
namespace App\Http\Controllers;
use App\CloseDayBreakdownCall;
class CloseDayBreakdownController extends Controller {
public function apiBulkInsert($cdbCall = null) {
DB::disableQueryLog();
try {
DB::beginTransaction();
$callId = $cdbCall->id ?? -1;// Call ID
$jsonData = FileHandler::getDownloadTaboola($filename);
if (empty($jsonData)) {
Log::debug(__METHOD__ . '; CDBCall no json file exists.');
// Update the calls table as NO json data is available from the file
CloseDayBreakdownCall::updateJsonToDBStatus([
'id' => $callId,
'storeDbStatus' => 4,
'remarks' => 'file do not exists.',
]);
/**
* DB::commit(); line was missing and does not updating the records in DB
*/
DB::commit();
return;
}
.... more logic to inert in bulk
DB::commit();
} catch (\Exception $e) {
// Update the calls table as all data inserted correctly
CloseDayBreakdownCall::updateJsonToDBStatus([
'id' => $callId,
'storeDbStatus' => 0,
'remarks' => $e->getMessage() . PHP_EOL
]);
Log::debug(__METHOD__ . '; Catch Exception; ' . $e->getMessage());
DB::rollback();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment