Welcome Guest, Not a member yet? Register   Sign In
DataException: There is no data to update after using save()
#1
Exclamation 

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

There is hasChanged() method in Entity.
https://codeigniter4.github.io/CodeIgnit...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.
Reply
#3

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
Reply
#4

(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.
Reply
#5

(06-01-2023, 02:06 AM)kenjis Wrote: There is hasChanged() method in Entity.
https://codeigniter4.github.io/CodeIgnit...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.
Reply
#6

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....
Reply
#7

try/catch the exception.
E.g.,
https://github.com/codeigniter4/shield/b...#L279-L299
Reply
#8

(This post was last modified: 01-12-2024, 08:27 AM by Memurame.)

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.'));
Reply
#9

(This post was last modified: 01-25-2024, 01:15 AM by kenjis.)

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

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 (.

-joho
Reply




Theme © iAndrew 2016 - Forum software by © MyBB