Project

General

Profile

Wt::Dbo - Mapping primary key to a class attribute

Added by Rajeev Gajbhiye about 12 years ago

Hi,

I want that the primary key (id) field should map to an attribute, say id, in my class. I know that I can customize dbo_traits, and use dbo::id(). But doing so means that it won't autoincrement. My requirement is only to capture it as attribute in class and let it be auto incremented one.

Also, if I like to have more columns to be indexed (and not just primary key). Is there a way to do so within Wt::Dbo? Or will I have to do it separately after createTables() has succeeded.

Regards,

Rajeev.


Replies (6)

RE: Wt::Dbo - Mapping primary key to a class attribute - Added by Koen Deforche about 12 years ago

Hey,

1) When inheriting a class from 'Wt::Dbo::Dbo', you can read the surrogate id from id(), there is nothing to gain from copying it into a variable ?

2) No, there is not (yet?) a way to tell Wt::Dbo to create more indexes. It has been suggested a few times, so perhaps a feature request is due.

Regards,

koen

RE: Wt::Dbo - Mapping primary key to a class attribute - Added by Rajeev Gajbhiye about 12 years ago

Hello,

Thanks for your response. Unfortunately, I was without computer and internet connection for long time, so could not try and respond. I didn't knew about solution (1). I tried it, however, on fetching all id's are being printed as --1, though in table they are assigned appropriate ids. I am using MySql backend. Have attached the code. Regarding (2), I will put up feature request.

Regards,

Rajeev.

RE: Wt::Dbo - Mapping primary key to a class attribute - Added by Saif Rehman about 12 years ago

Changing ID can not be done by surrogate key Wt::Dbo::ptr<>::id() function

RE: Wt::Dbo - Mapping primary key to a class attribute - Added by Rajeev Gajbhiye about 12 years ago

I was trying to read id and not print it. Anyway, I was able to solve the problem. Earlier I was declaring class Student as follows:

class Student : public Wt::Dbo::Dbo{

....

....

};

which should actually be as follows:

class Student;

class Student : public Wt::Dbo::Dbo<Student{

....

....

};

Perhaps, example on Cat (http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html) should be corrected to account for template parameter with Wt::Dbo::Dbo.

Also, deriving class Student from a parametrized class over Student itself seemed bit counter-intuitive to me. I expected it to lead to class definition of infinite depth.

RE: Wt::Dbo - Mapping primary key to a class attribute - Added by Rajeev Gajbhiye about 12 years ago

I just inferred that to access id , inheriting from Wt::Dbo::Dbo is not necessary. On accessing dbo object, it is returned through Wt::Dbo::ptr. The class ptr directly provides id() method. Hence, if the interest is only on object's id and not that it should be accessible through the object itself, then this approach might be preferable.

RE: Wt::Dbo - Mapping primary key to a class attribute - Added by Anonymous about 12 years ago

I've had a similar problem, and had help from this thread.

I was wondering if you have tried to apply any dbo_traits (http://www.webtoolkit.eu/wt/doc/reference/html/structWt_1_1Dbo_1_1dbo__traits.html) to data classes while inheriting from Wt::Dbo::Dbo? I get a compile error if I try this. Kind of easy to reproduce, just create a data class which inherits from Wt::Dbo::Dbo, and then follow the example here: http://www.webtoolkit.eu/wt/doc/tutorial/dbo/tutorial.html#_changing_or_disabling_the_version_field. I think the compile error occurs because an instance of the data class is created when supplying it to Wt::Dbo::Dbo<DataClass>, and the traits cannot be applied after that.

    (1-6/6)