CodeIgniter Forums
DataException: There is no data to update after using save() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: DataException: There is no data to update after using save() (/showthread.php?tid=87742)

Pages: 1 2


DataException: There is no data to update after using save() - bgeneto - 05-26-2023

I would like to discuss a very common situation that often arises:

1. A user updates their profile information in a form on the view page.
2. The controller receives the posted data, fills the profile entity with the new (or not) information using the `fill()` method, and then saves the data.
3. However, there is a concern with this procedure. When using `CodeIgniter\Entity\Entity::class`, only the attributes that have changed are updated. Therefore, if the user clicks the "Update" button without making any changes, a `DataException` is triggered with the message "There is no data to update."

In my opinion, this limitation renders entities almost useless for this type of workflow. Since we cannot determine beforehand if all the attributes of the `CodeIgniter\Entity\Entity` are the same as the original, there isn't much we can do to prevent this from happening. My question is: is this intended behavior? Shouldn't entities be able to return true even if nothing has changed?

We already have `$model->allowEmptyInserts()->insert([]);`, why not provide also both `$model->allowEmptyUpdates()->update([]);` and `$model->allowEmpty()->save([]);`?

Please enlighten me if a better workflow can be used in this case.

Regards,

Bernhard


RE: DataException: There is no data to update after using save() - kenjis - 06-01-2023

There is hasChanged() method in Entity.
https://codeigniter4.github.io/CodeIgniter4/models/entities.html#checking-for-changed-attributes
The method may return incorrect value, but in your use case, it seems to work.

I'm okay that we have a way to update with no data.


RE: DataException: There is no data to update after using save() - davis.lasis - 06-01-2023

I hate this exception.
In most of cases i just want to grab data, fill entity and update the DB row.

From UX point of view - really often users just click around form fields, actually changes nothing, but still press SAVE button. It's just the way it is.

Can't remember exactly, but i have had multiple cases when updated_at is not triggered as changed or is not actually changed, so this still triggers exception.

There MUST be an option to turn it off, at least manually - that way dev has full control of what he is doing.
From my point of view - this exception should be removed at all.

CORE functionality should never limit my needs or actions. It's dev's responsibility how he uses the code.

Thanks


RE: DataException: There is no data to update after using save() - kenjis - 06-01-2023

(06-01-2023, 01:10 PM)davis.lasis Wrote: There MUST be an option to turn it off, at least manually - that way dev has full control of what he is doing.
From my point of view - this exception should be removed at all.

CORE functionality should never limit my needs or actions. It's dev's responsibility  how he uses the code.

I agree. That seems more appropriate for CI.


RE: DataException: There is no data to update after using save() - bgeneto - 06-02-2023

(06-01-2023, 02:06 AM)kenjis Wrote: There is hasChanged() method in Entity.
https://codeigniter4.github.io/CodeIgniter4/models/entities.html#checking-for-changed-attributes
The method may return incorrect value, but in your use case, it seems to work.

I'm okay that we have a way to update with no data.

I missed the fact that `hasChanged()` without parameter checks the whole entity for changed values, it helps a lot. But I fully agree with @davis.lasis and ask you kindly to consider removing this exception or at least allow to disable it manually. 

Here are some more related threads in this forum about this:

https://forum.codeigniter.com/showthread.php?tid=86276
https://forum.codeigniter.com/showthread.php?tid=82165

Thanks in advance.


RE: DataException: There is no data to update after using save() - sjender - 11-14-2023

I was wondering if there is already a solution for this (unwanted) behaviour?
I am facing the same issues.

My users often just click save without actually saving anything. I don't want to 'Whoops' them every time this is the case....


RE: DataException: There is no data to update after using save() - kenjis - 11-14-2023

try/catch the exception.
E.g.,
https://github.com/codeigniter4/shield/blob/368274b14af4aecda4542c17028ad91d100d5635/src/Models/UserModel.php#L279-L299


RE: DataException: There is no data to update after using save() - Memurame - 01-12-2024

Is there any more information now?
I would also like to check the entries for changes before I execute the update function.

I have many form fields where I want to check whether the user has made a change or not.

Excerpt from my code

Code:
$testimonial = $testimonialModel->find($id);
$testimonial->lastname = $this->request->getPost('lastname');
$testimonial->email = $this->request->getPost('email');
$testimonial->data = json_encode($data);
//.....

if ($testimonial->hasChanged()){
    if (! $testimonialModel->save($testimonial)){
        return redirect()->back()->withInput()->with('msg_errors', $testimonialModel->errors());
    }
    return redirect()->back()->with('msg_success', lang('Das Formular wurde erfolgreich übermittelt. Vielen Dank.'));
}

return redirect()->back()->with('msg_info', lang('Es wurden keine Änderungen erkannt.'));



RE: DataException: There is no data to update after using save() - kenjis - 01-24-2024

I sent this PR: https://github.com/codeigniter4/CodeIgniter4/pull/8455
Any comments are welcome.


RE: DataException: There is no data to update after using save() - joho - 01-30-2024

Possibly also add some more details for the DataException exception? I can only find a brief comment about this in the documentation.

I actually landed on this thread because I was catching a DatabaseException in my try{}catch{} block, but it was a DataException that was thrown on a model update call (.