Project

General

Profile

Wt::Dbo OneToOne Relation is Confusing

Added by Aaron Wright almost 3 years ago

I've been trying to create a OneToOne relationship using Wt::Dbo and by following the tutorial. Unfortunately the tutorial says, "At the owning side, we add a weak reference to its settings, and in the persist() method we call hasOne()." This makes no sense to me. Why would the owning side have a weak reference? It's the owning side. It should have the Wt::Dbo::ptr object.

The ManyToOne and ManyToMany relationships seem to make sense, but the OneToOne is very confusing. I feel like I need to reverse what the tutorial is saying.

Am I missing something?


Replies (3)

RE: Wt::Dbo OneToOne Relation is Confusing - Added by Korneel Dumon almost 3 years ago

This is in line with how ManyToOne works. There it is also the side that has belongsTo which is pointing to the owner. Hopefully this helps you make sense of it.

RE: Wt::Dbo OneToOne Relation is Confusing - Added by Roel Standaert almost 3 years ago

Yes, the wording is rather confusing, but it's correct:

  • the hasOne side uses a weak_ptr
  • the belongsTo side uses ptr

Dbo uses directional wording, even though there's not necessarily a directional relationship, or the direction may even be the other way around. It's kind of just an idiosyncrasy. I don't know how it could be "fixed".

An important thing to know is that it's always the belongsTo side in a ManyToOne or OneToOne relationship that contains the column referring to the other table (even though in the case of OneToOne you may expect it to be the other way around).

RE: Wt::Dbo OneToOne Relation is Confusing - Added by Aaron Wright almost 3 years ago

The part that was causing me problems was that the ownership concept didn't seem present in the C++ code, despite what the tutorial said, because of the weak pointer. For example, I have an object that owns another object; easy enough, use hasOne, right? Well, then I need to have a weak pointer in that object, and every time I access that object it is doing a query or I have to call the lock function to get a normal pointer to the owned object. This doesn't seem like ownership. That seems like the opposite of ownership. A weak pointer is the opposite of ownership. Weak pointers are generally used by child objects to point back to their parents, as child objects do not own their parents.

When I used the ManyToOne relationship, it seemed like the weak pointer wasn't involved at all, which was nice, but perhaps the Wt::Dbo::collection was hiding that from me.

    (1-3/3)