Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving an entity with a modified field which excludes them from model results in error #967

Closed
mkrecek234 opened this issue Feb 22, 2022 · 4 comments

Comments

@mkrecek234
Copy link
Contributor

Steps to reproduce:

  • You have a model wit emails "Email" and filter it, e.g. by addCondition(unread, true);
  • Out of this model you edit an email, changing "unread" to false, and apply an entity->save(['unread' => false]);
  • Saving would theoretically work, but as atk4/data by default reloads the entity after save, it is rolled back with an error because that entity no longer exists in the model.

Current workaround: change reaload_after_save of model to false.

Problem for developers:
Saving values to entity which might result that they no longer are part of a model with conditions happens regularly.

Idea:
The reload will be performed to the model without conditions to ensure entity can be found.

@mvorisek
Copy link
Member

Model is currently a domain/scope, eg. violation any conditions must always throw/rollback and never allow to save out-of-scope model data.

What you need is to have a Model /wo such filtering conditions and then close it for filtering an unread messages. But when saving such message as read, you must use the unfiltered model or remove the filtering cond before save.

Please post also the traversing back and forth case which might be solveable, but here is nothing we can do.

The only thing I think we can do is introduce a flag to every condition marking if such condition is filtering one or a model/hard one, if non-hard, we can ignore such condition on save.

@mvorisek
Copy link
Member

Even if we will ignore conditions set outside Model::init(), it is not logical to be able to save a record that cannot be selected later

Typical usecase is softdelete. I belive such usecase, and answer/solution for you?, is to temporary relax/ignore such condition during save in try&finally (remove&restore) approach

@mkrecek234 mkrecek234 reopened this Aug 25, 2022
@mkrecek234
Copy link
Contributor Author

Reopening as in Discord discussion. Also see branch https://github.com/atk4/data/tree/save_wo_scope

@mvorisek
Copy link
Member

I am closing this issue. We can discuss it under your PR. No need for 2 places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants