ManyToOne relation

Forum for posting problems using QxOrm library

ManyToOne relation

Postby mtfk » Tue May 03, 2011 1:48 pm

Hi,

I have a little problem with save realtion ManyToOne
it look like that:

Document - Language

Document has one Language
Language has many Docuemnt

I have try something like that:

Code: Select all

lang = new Language();
lang->name = "English";

document = new Document();
document->language = lang;

document.save(); // <- is the same like qx::dao::save(document)



But it dose not work, document is saved with very strange language_id (random). But language is saved correct.
I think this is because language is saved after saved document

I have tried to save language before document and it works.

Like that:

Code: Select all

lang = new Language();
lang->name = "English";

lang.save(); // is the same like qx::dao::save(lang);

document = new Document();
document->language = lang;

document.save(); // <- is the same like qx::dao::save_with_all_relation(document)



So question: Do I need save lang before document? Or it should work using only save_with_all_relation on document?

I will be very greatfull for any example that work. Those example on qxOrm website work but all object are saved before relation so I am not sure if this is possible.

Thanks a lot for any clue.
mtfk
 

Re: ManyToOne relation

Postby QxOrm admin » Tue May 03, 2011 7:21 pm

Hi,

I think this is because language is saved after saved document

You are right : language will be inserted/updated after document (you can see it in the file "./include/QxDao/QxSqlRelation_ManyToOne.h" and methods "onBeforeSave()" and "onAfterSave()")

Your problem : you don't know language id before to save a document (and in your document table, you have to know a langage id) :
1- you can manage your langage id manually without using "autoincrement" feature.
2- or you can save language (to get last insert id) before to save a document.

What is the difference between "qx::dao::save(document)" and "qx::dao::save_with_all_relation(document)" :
1- "qx::dao::save(document)" will save only properties of document class (so only document table will be modified into database)
2- "qx::dao::save_with_all_relation(document)" will save first the document class (so document table will be modified into database), then it will save language class (so language table will be modified into database too)

Maybe you can use also QxOrm's triggers to save language before document :
http://www.qxorm.com/qxorm_en/faq.html#faq_130
QxOrm admin
 


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 3 guests