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

(This post was last modified: 10-26-2024, 10:34 AM by mhmmd.)

(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:
$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.
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
Reply


Messages In This Thread
RE: how to check if update on model did succeed? - by mhmmd - 10-26-2024, 10:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB