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

Add weak conditions/save support #1058

Closed
wants to merge 3 commits into from
Closed

Add weak conditions/save support #1058

wants to merge 3 commits into from

Conversation

mkrecek234
Copy link
Contributor

@mkrecek234 mkrecek234 commented Aug 25, 2022

related initial issue #967

@mvorisek
Copy link
Member

Usecases? What is "weak" and when? Sometimes traversing should be weak, sometimes not, we should probably start with all possible usecases and then find a well understandable solution.

*
* @return $this
*/
public function tryReload()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the usecases? If a record is expected to be possibly deleted, you can always check with tryLoad or handle reload exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case is as follows - consider a Crud which displays unread messages with addWeakCondition('unread', true) . You click on edit, and change the unread flag to false. You click save.
The crud then runs a normal ->save function. Since save allows that - in case a field is changed that moves it outside of a weak condition, the save command should only try to reload. If it is successful, the crud will be updated, if it is null, then the entity left the scope, and thus the same animation as in a delete function will be run.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is legit usecase 👍

@@ -1562,7 +1651,7 @@ public function save(array $data = [])
if ($this->idField && $this->reloadAfterSave) {
$d = $dirtyRef;
$dirtyRef = [];
$this->reload();
$this->tryReload();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no try - we want strict behaviour as much as possible, not to silently ignore possible issues

Copy link
Contributor Author

@mkrecek234 mkrecek234 Aug 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a possible issue but a designed situation - if you do not have any weak condition then we could think about throwing an exception and not null. I think we should not be too dogmatic, especially if we destroy then logics which are present in Ui.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand this, please provide a testcase for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I placed the tryReload for the usecase above (the crud remove unread message from scope on save) - so when a model saves something, the associated view gets a feedback if the entity is still there (in scope, so not null) or if it is gone - so save function only tries to reload, and if it is not successful it should return null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but we cannot remove this check because of some need.

I have no solution is my head, maybe we need to add some support to ui for it.

In atk4/data, we have currently BEFORE_SAVE and AFTER_SAVE hooks, maybe we need some concept of try/finally SAVE hook.

In the past we discussed an immutable models. I am still not fully decided if a condition removal should be supported at all. I have no plan to disallow it in the near future, but to support the conditions removal fully, we need to implement #662 first.

If you want to pursue this feature, please start with a real Crud test in atk4/ui. If you need one or two atk4/data classes modified there, you can extend the atk4/data classes in atk4/ui. Please keep in mind, this feature should work also for other ui components like Multiline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not rund a proper reload if no weak condition exists - only if a weak condition exists, you only try to reload and return null of it left the scope.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we need to be able to rely on reload. If the record became out of scope, it can be some error as well, with tryReload, we can be left with old data.

@mvorisek
Copy link
Member

please make the CS/stan CI happy and unit testing to finish at least

this is good practice so we can focus on the real issues

@mvorisek mvorisek changed the title Allow saving resulting entity to drop out of weak conditions of model's scope Add weak conditions/save support Aug 27, 2022
@mvorisek
Copy link
Member

mvorisek commented Jan 7, 2023

see

// $c->insert([

I belive a weak approach is bad - any condition (added by an user) will still break the (currently commented) hook code. Maybe a solution can be to backup the conditions after init is invoked and allow it to be used like in #1054 invokeCallbackWithoutUndeletedCondition.

@mvorisek
Copy link
Member

closing in favor of #1054

@mvorisek mvorisek closed this Aug 12, 2023
@mvorisek mvorisek deleted the save_wo_scope branch August 12, 2023 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants