Page 1 of 1

How to update object with collection of another objects?

PostPosted: Thu Feb 04, 2016 4:49 pm
by staszek_dev
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?

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

PostPosted: Thu Feb 04, 2016 5:40 pm
by qxorm
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 ?

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

PostPosted: Mon Feb 08, 2016 12:31 pm
by staszek_dev
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.

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

PostPosted: Tue Feb 09, 2016 10:21 am
by qxorm
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());

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

PostPosted: Wed Feb 10, 2016 9:21 am
by staszek_dev
Ok, it works pretty :)
Thank you so much!

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

PostPosted: Wed Feb 10, 2016 3:07 pm
by qxorm
it works pretty :)

Great !