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
#2

update() - Returns (bool) - true on success, false on failure.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(05-22-2023, 12:12 AM)InsiteFX Wrote: update() - Returns (bool) - true on success, false on failure.
Yes, it returns true for successfull running update.
But it return true allthough if no lines have been altered.
That is my Problem.
Reply
#4

$this->db->affectedRows()
Reply
#5

(This post was last modified: 05-23-2023, 08:09 AM by StefanKittel.)

Hello kenjis,

> $this->db->affectedRows()
Undefined property: App\Controllers\CliController::$db

PHP Code:
File CLIController.php

$db 
db_connect();
$JobModel_obj = new JobModel();

$UpdateResult $JobModel_obj
                
->where('id'$JobFound['id'])
                //->where('status', JOB_STATUS_IDLE)
                ->where('status'5)
                ->set(['status' => JOB_STATUS_STARTED])
                ->update();

echo 
"4=" $db->affectedRows() . "\n"


shows 4=1 when no update has been done
Reply
#6

It is recommended put all DB related code in models, but if you call it in a controller:
$JobModel_obj->affectedRows();

But you need to call affectedRows() (this is actually a method in a DB connection) right after calling the update statement. Check the all SQL statements that executed.

If your Model calls another SQL statement inside, then executing affectedRows() afterwards will not return the correct value.
In such a case, you need to use Query Builder instead of the Model method.
https://codeigniter4.github.io/CodeIgnit...ilder.html
https://codeigniter4.github.io/CodeIgnit...ry-builder

Model and Query Builder are different things, but they are mixed. So if you don't understand
the concepts well, you may be confusing.
Reply
#7

thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB