Project

General

Profile

Wt::Dbo

Added by Thomas Saquet about 12 years ago

Hello,

I'm trying to create a primary key from a foreign key and another value, following the tutorial.

I created a struct for the primary key :

struct PrimaryKey
{
    int itemId;
    Wt::Dbo::ptr<Organization> orga;

    PrimaryKey(int id, Wt::Dbo::ptr<Organization> ptr)
        : itemId(id), orga(ptr) { }

    PrimaryKey(){ }

    [...]
};

Then i overloaded the field() function, and i tried to use my type as a primary key :

class Item
{
    public:
        Item();
        virtual ~Item();


        PrimaryKey pk;

        template <class Action>
        void field(Action& a, PrimaryKey& pk,
                   const std::string& name, int size = -1)
        {
            field(a, pk.itemId, "ITE_ID");
            field(a, pk.orga, "ORG_ID");
        }

        template<class Action>
        void persist(Action& a)
        {
            Wt::Dbo::id (a, pk, "PRIMARY_KEY_TEST");

            Wt::Dbo::belongsTo(a, pk.orga, "FOREIGN_KEY_TEST");

            [..]
        }

};

When i compile, i have :

 /usr/local/include/Wt/Dbo/DbAction_impl.h:456:5: erreur: no matching function for call to ‘Wt::Dbo::MetaDbo<Probe>::setId(PrimaryKey&)’

I tried to put the const keywork in front of the declaration, but the error is coming after :

no matching function for call to ‘belongsTo(Wt::Dbo::TransactionDoneAction&, const Wt::Dbo::ptr<Organization>&, const char [17])’

I certainly missed something but i don't understand what. Thank you in advance for your help.

Thomas


Replies (8)

RE: Wt::Dbo - Added by Thomas Saquet about 12 years ago

... Forgot to complete the title, sorry.

Wt::Dbo foreign key in a composite primary key

RE: Wt::Dbo - Added by Saif Rehman about 12 years ago

I don't know about the compile error.

But there is another problem.

Fixed in this

class Item
{
    public:
        Item();
        virtual ~Item();

        PrimaryKey pk;

        template <class Action>
        void field(Action& a, PrimaryKey& pk,
                   const std::string& name, int size = -1)
        {
            field(a, pk.itemId, "ITE_ID");
            Wt::Dbo::belongsTo(a, pk.orga, "ORG_ID");
        }

        template<class Action>
        void persist(Action& a)
        {
            Wt::Dbo::id (a, pk, "PRIMARY_KEY_TEST");

            [..]
        }

};

RE: Wt::Dbo - Added by Thomas Saquet about 12 years ago

Ok thank you, I think I understand, i would certainly have had a problem about that after.

But it doesn't solve my current problem if anyone has a clue it would be great :)

RE: Wt::Dbo - Added by Saif Rehman about 12 years ago

If you could post a test file then I'll be able to reproduce it

RE: Wt::Dbo - Added by Saif Rehman about 12 years ago

Found and fixed the problem

namespace Wt
{
    namespace Dbo
    {
        template <class Action>
        void field(Action& a, PrimaryKey& pk,
                   const std::string& name, int size = -1)
        {
            field(a, pk.itemId, "ITE_ID");
            Wt::Dbo::belongsTo(a, pk.orga, "ORG_ID");
        }
    }
}

class Item
{
    public:
        Item();
        virtual ~Item();

        PrimaryKey pk;

        template<class Action>
        void persist(Action& a)
        {
            Wt::Dbo::id (a, pk, "PRIMARY_KEY_TEST");

            [..]
        }

};

You are supposed to overload the Wt::Dbo::field() function. What you did was created a function Item::field() in class Item.

RE: Wt::Dbo : new example - Added by Thomas Saquet about 12 years ago

Hello,

Thank you again for this new piece of code, but it doesn't solve my orignal problem and I now found out what happened.

The struct containing my composite key was not recognized as an IdType. This problem, and many others I had, were due to my lack of knowledges about c (include, forward declaration) and my will of separating classes and to not have everything in the same place.

I took the tutorial and put it in different files. It now compiles and works, you can find the source code here :

https://redmine.gayuxweb.fr/projects/wtdboex

It will remain online for beginner people like me.

RE: Wt::Dbo - Added by Koen Deforche about 12 years ago

Hey,

Thanks. This is useful indeed, I guess I never considered these issues which are indeed not evident, certainly not from the tutorial.

Care if I add this version as a Wt example to Wt itself ?

Regards,

koen

RE: Wt::Dbo - Added by Thomas Saquet about 12 years ago

Hello Koen,

No problem, first it is mainly your own source code and second it will be more helpfull added directly to wt than on my redmine :)

The database I work on is a bit larger (about 50 tables) and the source code becomes quite important. I will try to publish the source code once it is finished to provide a full example and to have your opinion about how complicated the "include management" is.

Regards,

Thomas

    (1-8/8)