Get surrogate id value from within the persist method
Added by Alex Ignatov over 6 years ago
Hi,
I have a class which is persisted to the database. It uses default surrogate id. Works absolutely fine. Now I add few members to this class which I do not want to persist to the database but to be calculated on the fly when the persisted members are loaded. I can do the calculation inside the persist method after Wt::Dbo::field() methods are executed and all persisted members are loaded. But I need the surrogate id value as well and cannot find a way to extract it. Any ideas on how to get it?
Alex
Replies (4)
RE: Get surrogate id value from within the persist method - Added by lm at over 6 years ago
First, make sure you realize that the persist method isn't only used to load objects from the database; it's used for several things.
Are you aware that you can make your business object extend Wt::Dbo::Dbo
https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html ? Then, you'll have access to this->id()
. This might not work, though: I'm not sure when that id()
function begins to show the right value; that is, I'm not sure if accessing it within the persist method will work properly.
It does seem however that the persist
template is the wrong place to do this. You only want to calculate these values once when they're needed? Or perhaps they are always dependent on other values in the class? I'm thinking they should either be lazy-loaded:
struct my_class : public Wt::Dbo::Dbo<struct my_class> {
std::string get_name() const { return name; }
std::string get_hash() const { if (!hash.size()) hash = std::to_string(id()) + name; return hash; }
private:
std::string name; /*persisted*/
std::string hash; /*transient*/
};
Or calculated on the fly:
std::string get_hash() const { return std::to_string(id()) + name; }
RE: Get surrogate id value from within the persist method - Added by Alex Ignatov over 6 years ago
Actually I can see that the surrogate id value is stored as the member of LoadDbAction object, passed to the persist() method. But it is a private member.
RE: Get surrogate id value from within the persist method - Added by lm at over 6 years ago
Ah, so perhaps you could specialize your template! I would not have thought of that, and always treated the template parameter as "opaque" and "magical". Of course, if you specialize on the LoadDbAction
, I assume you're giving up any guarantee of forward compatibility, but perhaps it's an acceptable risk at this point for you.
RE: Get surrogate id value from within the persist method - Added by Alex Ignatov over 6 years ago
Thanks Im at! I missed the possibility of deriving from the dbo::dbo class somehow. This solves the issue. Yes, I realise that persist is used for several things including inserting a new object into the database. That would be one more question if I can distinguish inside the persist method between different actions. But I still have to decide whether calculate on demand, on load from db or store in the db. Just wanted to know which possibilities do I have. Thanks once more!