atk4 / ui

Robust and easy to use PHP Framework for Web Apps
https://atk4-ui.readthedocs.io
MIT License
439 stars 104 forks source link

CRUD with updated value outside model condition #2204

Open nocelab opened 2 months ago

nocelab commented 2 months ago

I need to set a Crud with a records subset.
The Model has the combo field:

$this->addField('tipo', ['values' => ['x' => 'fix', '+' => 'new', '-' => 'removed']]);

and I add condition with:

$mod->addCondition($mod->expr("tipo = '+'"));

Crud show correctly the recordset but when I try to edit a row, changing tipo value, I get this error on saving:

Critical Error
Atk4\Data\Exception: Record with specified ID was not found

model: Modello
id: 3311
scope: 'expression 'tipo = '+'''
action: Atk4\Data\Model\UserAction (edit)
mkrecek234 commented 2 months ago

This is the situation by design as by changing that value the entity would drop out of the model.

See discussion here https://github.com/atk4/data/issues/1052

You need to make sure that onSubmit the model is unconditioned that is used to save the changed entity, and the conditioned model is only used to display the crud.

Agree that this it not very intuitive and good tips welcome what the best way to code it might look like

nocelab commented 2 months ago

Thanks... very happy that someone has a similar situation!!! I need to deep read the post to understand better if there is my answer... but it's sure the right way.

I also tried to modified demos/crud.php adding:

$model->addCondition($model->fieldName()->name, 'like', "I%");

saving one edited row (changing the Name with something not starting with "I") works good without error... but looking source code I don't realize why