Dbo::FormModel & Dbo::FormView - How to change the database item?
Added by Stefan Bn 6 months ago
Hello,
I'm in the process of understanding Dbo::FormModel and Dbo::FormView. The Wt example examples\dbo-form
shows the basic concept, however just uses a single database item.
What is the recommended way of switching to another database item that should be viewed/modified by the Dbo::FormView
?
My assumption would be to change it in the Dbo::FormModel
but I don't see any methods for this (just init/save). So the conclusion would be to entirely create a new instance of Dbo::FormModel
with the new database item in the constructor and then call Wt::Form::Dbo::FormView< C >::setFormModel
with the new model again? Is that how it is designed?
For examples\dbo-form
it would be something like this?
Wt::Dbo::Transaction t(session_);
Wt::Dbo::ptr<TestDboObject> item2 = session_.find<TestDboObject>().where("id=2");
std::shared_ptr<TestDboModel> model2 = std::make_shared<TestDboModel>(session_, item2);
testDboView->setFormModel(model2);
Thanks,
Stefan
Replies (3)
RE: Dbo::FormModel & Dbo::FormView - How to change the database item? - Added by Matthias Van Ceulebroeck 5 months ago
Hello Stefan,
As before, my apologies for being late, I am working through my backlog.
while it is possible to switch models, I don't think that's the best way to approach it.
Say you have an overview of multiple objects one is allowed to edit. Upon selecting the item, a model and view are constructed. This view can then be saved/closed.
When the view is closed, I would destruct the widget, and recreate a model and view for each new object.
It is very true that the Dbo::FormModel
is a wrapper around a Dbo object, and doesn't make the object interchangeable. So it should live as long as the item is being accessed/viewed/edited. After that it should be destroyed, and a new model should be created for each new item.
Personally, I often work with overviews, which are WTableViews
, contained in a tab widget that show the contents of a certain table (potentially under some conditions). Then clicking an edit button (perhaps as a delegate), or double clicking the entry will open the model/view in a new tab.
That of course is just a preference of mine. But perhaps a good example to add to Wt as well. I only fear that the example would be a bit "strict", and would lead developers to only use it in a similar fashion.
What do you think?
Best,
Matthias
RE: Dbo::FormModel & Dbo::FormView - How to change the database item? - Added by Stefan Bn 5 months ago
Thanks for the clarification Matthias!
It is very true that the Dbo::FormModel is a wrapper around a Dbo object, and doesn't make the object interchangeable. So it should live as long as the item is being accessed/viewed/edited. After that it should be destroyed, and a new model should be created for each new item.
This exact statement should be in the documentation of Wt::Form::Dbo::FormModel
! It fills a missing gap and everything would be clearer for developers.
What do you think?
Usually I do have a classic UI-layout, let's say with a WSelectionBox
on the left side, that shows all people in the database. On the right side I have the all the input widgets for the corresponding database fields for street, adress, city, tel etc. which are always present and shown and an update/save button.
The user might do a search query that leads to no results. Following the Wt-design, the Form::Dbo::FormView
GUI input widgets need to disappear instead of just being empty and disabled. So depending on the user actions, the Dbo::FormView
will appear and disappear all the time ..
I think this makes FormView/Dbo::FormModel
very special over all the other MVC views that consume a model using setModel()
(e.g. WComboBox, WTreeView, WTableView
). You assign the model once to the view and then just work on the model, where the view automatically follows. This was the scheme that I expected originally.
If you ask me, I personally would change Wt::Form::Dbo::FormModel
so that it can operate on different database objects. There is already the method Wt::WFormModel::reset()
that clears and invalidates the model/view. It just needs one additional method Dbo::FormModel::setObject(Wt::Dbo::ptr<C> ptr)
to change the model to a new database object. This would follow the design of all other MVC classes. I think most Dbo use-cases need to access/modify many different objects (that's why we use a database). So destroying/recreating model and view for every single object seems not very straigtforward to me ..
Stefan
RE: Dbo::FormModel & Dbo::FormView - How to change the database item? - Added by Matthias Van Ceulebroeck 5 months ago
I have created two tickets to track this.
For now, I have made them private, as this will trigger some internal discussion. I will make them publicly visible as soon as there is an agreement/compromise.
The model and view, in my opinion, have been created with certain projects of ours in mind. This lead to certain assumptions being made, but never to the consideration whether these actually are valid assumptions, or whether we should look beyond them.
I can completely understand your point, and have to admit we did not consider this design/architectural approach. Same as above, we will take a look at how this would impact the current system, and whether this can be easily integrated or not.
Thank you for the feedback. I really do appreciate it.
Best,
Matthias.