How to update object with collection of another objects?

Forum for posting problems using QxOrm library

How to update object with collection of another objects?

Postby staszek_dev » Thu Feb 04, 2016 4:49 pm

Hi,
I have object A which is in relation one-to-many with object B.
Object A has QList<QSharedPointer<B>> and object B has QSharedPionter<A>.
Now, if I insert object A1, B1, B2, B3, and then I add objects B1, B2, B3 to Qlist in object A1 and call update_with_all_relation(A1), the relation isn't persist.
But if I set in B1 QSharedPionter<A> = A1, and update_with_all_relation(B1), the foreign key is save.

this doesn't work
Code: Select all
A1->m_ListB.append(B1);
A1->m_>ListB.append(B2);

qx::dao::update_with_all_relation(A1);


this work
Code: Select all
B1->m_A.reset(A1.data());
B2->m_Areset(A1.data());

qx::dao::update_with_all_relation(B1);
qx::dao::update_with_all_relation(B2);


Somebody have any idea, what is wrong?
staszek_dev
 
Posts: 4
Joined: Thu Feb 04, 2016 4:02 pm

Re: How to update object with collection of another objects?

Postby qxorm » Thu Feb 04, 2016 5:40 pm

Hello,

Did you try the function qx::dao::save_with_all_relation() instead of qx::dao::update_with_all_relation() ?
Do you have the same result ?
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: How to update object with collection of another objects?

Postby staszek_dev » Mon Feb 08, 2016 12:31 pm

Hi,
Thank you for your suggestion, but it doesn`t work.
Invoking qx::dao::save_with_all_relation() and qx::dao::update_with_all_relation() qxorm log slq statement witch include update statement for table B, but there are no changes in DB.
staszek_dev
 
Posts: 4
Joined: Thu Feb 04, 2016 4:02 pm

Re: How to update object with collection of another objects?

Postby qxorm » Tue Feb 09, 2016 10:21 am

Ok I'm able to reproduce it with the qxBlog sample project (where you have a 1-n relationship between author and blog).
There is a parameter to fix your issue : qx::QxSqlDatabase::getSingleton()->setForceParentIdToAllChildren(true);
This is not the default behavior because of performance reason but you can enable it if you want to use this feature.
Just apply this parameter and it should be ok.

Here is my test with qxBlog sample project (cpoy/past at the end of the main function) :
Code: Select all
   qx::QxSqlDatabase::getSingleton()->setForceParentIdToAllChildren(true);

   blog_ptr bb1; bb1.reset(new blog()); bb1->m_text = "bb1"; daoError = qx::dao::insert(bb1); qAssert(! daoError.isValid());
   blog_ptr bb2; bb2.reset(new blog()); bb2->m_text = "bb2"; daoError = qx::dao::insert(bb2); qAssert(! daoError.isValid());
   blog_ptr bb3; bb3.reset(new blog()); bb3->m_text = "bb3"; daoError = qx::dao::insert(bb3); qAssert(! daoError.isValid());

   author_ptr aa1; aa1.reset(new author()); aa1->m_id = "aa1"; aa1->m_name = "aa1"; daoError = qx::dao::insert(aa1); qAssert(! daoError.isValid());

   aa1->m_blogX.push_back(bb1);
   aa1->m_blogX.push_back(bb2);
   aa1->m_blogX.push_back(bb3);
   daoError = qx::dao::update_with_all_relation(aa1); qAssert(! daoError.isValid());
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: How to update object with collection of another objects?

Postby staszek_dev » Wed Feb 10, 2016 9:21 am

Ok, it works pretty :)
Thank you so much!
staszek_dev
 
Posts: 4
Joined: Thu Feb 04, 2016 4:02 pm

Re: How to update object with collection of another objects?

Postby qxorm » Wed Feb 10, 2016 3:07 pm

it works pretty :)

Great !
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 8 guests