![]() |
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 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))";
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 )
$this->db->affectedRows()
Hello kenjis,
> $this->db->affectedRows() Undefined property: App\Controllers\CliController::$db PHP Code: File CLIController.php shows 4=1 when no update has been done
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.
(05-23-2023, 03:46 PM)kenjis Wrote: It is recommended put all DB related code in models, but if you call it in a controller:Thank you for your reply @kenjis . But it would be very nice to have following info on official codeigniter docs website - update() method returns `true` 1. On successfull `update()` 2. When no rows affected. For example ```PHP $this->userEntity->setPassword($data['password']); $update = $this->userModel->update($userID, $this->userEntity); if($update) { // .. } ``` A developer can easily forget to check if `$userID` is exists. I couldn't find this usage anywhere else. Also thank you for asking this question @StefanKittel |
Welcome Guest, Not a member yet? Register Sign In |