Welcome Guest, Not a member yet? Register   Sign In
how to check if update on model did succeed?
#1

(This post was last modified: 05-21-2023, 04:24 PM by StefanKittel.)

Hello,
I have a basic model with no configuration else of allowed fields.
I want to update a row need and know if the update was done or not and can not figure it out.
How can I get the number of affected lines for this update?
I don't want to break CI4 by running the command as MySQL plain.
should be zero if not matched.
This is to ensure the job ist not run twice at the same time.
thanks
Stefan
PHP Code:
//search first jobs with status is idle
$JobFound $JobModel_obj
->where('status'JOB_STATUS_IDLE)->first();

//if no idle job is found, exit
if (!$JobFound)
{
$this->LogController_obj->AddToLog_CLI("No Jobs found");
return;
}

//update jobstatus from idle to starting
//doing this in one command ensures to second thread run the same jobs
$UpdateResult $JobModel_obj
    
->where('id'$JobFound['id'])
    //->where('status', JOB_STATUS_IDLE)
    ->where('status'5)
    ->set(['status' => JOB_STATUS_STARTED])
    ->update();

echo 
$UpdateResult "\n";
echo 
$JobModel_obj->countAllResults(false) . "\n";
$db = \Config\Database::connect();
echo 
$db->affectedRows() . "\n"

Hello,
short: the sql command itself works fine.
But i prefere to use CI4 for this.
PHP Code:
$SQLString "UPDATE job SET status=" JOB_STATUS_STARTED " WHERE ((id=" $JobFound['id'] . ") AND (status=5))";
$db->query($SQLString);
echo 
$db->affectedRows() . "\n"
Reply


Messages In This Thread
how to check if update on model did succeed? - by StefanKittel - 05-21-2023, 04:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB