Project

General

Profile

[Wt 3.3.3] hasOne relationship from one object toward itself

Added by Thomas Saquet over 9 years ago

Hello,

I try to make a relationship from one object toward itself.

I think I may have a problem with the usage of weak_ptr. The relationship created seems inverted.

I declare the relationship like this:

Wt::Dbo::weak_ptr<MyClass> myClass;
Wt::Dbo::ptr<MyClass> myClassBelongTo;

template<class Action>
void persist(Action& a)
{
    Wt::Dbo::belongsTo(a, myClassBelongTo, TRIGRAM_MY_CLASS SEP "NXT_ID");
    Wt::Dbo::hasOne(a, myClass, TRIGRAM_MY_CLASS SEP "NXT_ID");
}

I have two objects MyClass : myClass1 and myClass2.

If I do "myClass1.modify()->myClass = myClass2;" then the myClass2 line in the database has a reference to myClass1.

What I expected was the opposite. What do you think about that ? Am I wrong ?

Thank you,

Regards,

Thomas

P.S. : I understand that I have to use myClassBelongTo to have the expected relationship, but it does not really make sense, does it ?


Replies (2)

RE: [Wt 3.3.3] hasOne relationship from one object toward itself - Added by Koen Deforche over 9 years ago

Hey,

The 'belongsTo()' and 'hasOne()' names indeed are not always intuitive. From a database perspective, there's no difference between Many-to-One (hasMany()/belongsTo()) and One-to-One (hasOne()/belongsTo()) and we chose to keep the same database organization as a consequence. In Many-to-One it's the belongsTo() side that has the actual pointer value and hasMany() is mapped by a query. And that's why hasOne() is the query and belongsTo() is the pointer, in a One-to-One relation.

We often have a discussion here as well on the names (which I took from Ruby-on-Rails ;-) ).

Regards,

koen

RE: [Wt 3.3.3] hasOne relationship from one object toward itself - Added by Thomas Saquet over 9 years ago

Hello Koen,

Thank you for this answer, I understand why you made that choice :) I guess it can be an endless source of discussion as there is no really perfect solution.

Yes, I noticed it was the same than RoR (which I discovered recently), I thought it was kind of conventional, I think that is well inspired ;)

Regards,

Thomas

    (1-2/2)