Project

General

Profile

Is there a way to create a 'Wt::Dbo::ptr' from 'this' pointer?

Added by Plug Gulp about 2 years ago

Say I have Dbo classes Mother and Child. The relation between them is One-to-many. The Mother class has a method create_child. In this method a new Child object is created. The constructor of the Child class takes Wt::Dbo::ptr<Mother> as a parameter. One way I can get hold of the Wt::Dbo::ptr<Mother> pointer from within the Mother::create_child method is to query the database. But that is expensive. I am already inside the Mother object, so is there a way to easily convert the this pointer to a Wt::Dbo::ptr instead of querying the database again?


Replies (1)

RE: Is there a way to create a 'Wt::Dbo::ptr' from 'this' pointer? - Added by Roel Standaert about 2 years ago

The easiest way would probably be to have Mother derive from Wt::Dbo::Dbo<Mother>: https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html

Then you can simply use self().

If you have the id of the Mother object, then Session::load should also simply get the object from the session's cache. Session::loadLazy can also be used to guarantee that no actual query is performed to create the ptr. Only if the ptr is then dereferenced and the object is not in the session's cache, it will query the database.

    (1-1/1)