clone_with_relation_recursive

Open discussion on QxOrm library

clone_with_relation_recursive

Postby Kasuax » Tue Jun 28, 2016 3:07 pm

Is there a way I could go about implementing a function that extends the existing qx::clone in a recursive nature? Like save_with_relation_recursive but clone_with_relation_recursive?
I feel like all of the relationships are mapped out well enough that this could be implemented in an elegant and recursive nature. This is of course assuming that all of the keys are integers and the database does all the counting...

Curious if anyone has thought about this...

Thanks!
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: clone_with_relation_recursive

Postby qxorm » Tue Jun 28, 2016 7:49 pm

Hello,

implementing a function that extends the existing qx::clone in a recursive nature

The qx::clone function is already recursive : all levels of relationships should be cloned.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: clone_with_relation_recursive

Postby Kasuax » Tue Jun 28, 2016 8:15 pm

The qx::clone function is already recursive : all levels of relationships should be cloned.

I assume means all relationships bound to that object are cloned but nothing after that. Relationship hops 2 through n indicate the need for a special recursive clone. Is that correct?

The biggest challenge I'm having at the moment (whilst implementing my own recursive clone), is how to manage the IDs more effectively. If I clone my top level obj and give it a new ID, I have to iterate over nearly everything and update their own reverse relationship entry so that they reference the new object and not the old one.

The problem is compounded further when I have an object with multiple n-1 and 1-n relationships that is coming from xml persistence. I don't understand when attempting clone how I'm supposed to manage/re-index all of the relationships so that when pushed to a database this object looks like an entirely new instance, nothing shared.

Make sense? I'm not sure it's the typical use case for this framework but the ability to replicate a file obj repeatedly as a new instance in my db is essential.

Thanks in advance!
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: clone_with_relation_recursive

Postby qxorm » Wed Jun 29, 2016 8:59 am

Ok, I think I understand now : the qx::clone() function doesn't interact with database (it is not in the qx::dao namespace ;) ).
You want to create a clone (+ all levels of relationships) in database.

Did you try to use the qx::dao::save_with_relation_recursive() function with qx::dao::save_mode::e_insert_only as a second parameter ?
I think it should work if all your primary keys are auto-increment (if it is integer type, then it is auto-increment by default).

Something like this :
Code: Select all
QSqlError daoError = qx::dao::save_with_relation_recursive(myInstance, qx::dao::save_mode::e_insert_only);
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: clone_with_relation_recursive

Postby Kasuax » Wed Jun 29, 2016 1:58 pm

Did you try to use the qx::dao::save_with_relation_recursive() function with qx::dao::save_mode::e_insert_only as a second parameter ?
I think it should work if all your primary keys are auto-increment (if it is integer type, then it is auto-increment by default).


I was going to try this today. One of my primary keys is a string so I was thinking about altering it. That same table also has n-n relationships with another and I think it's still going to squawk on something.
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: clone_with_relation_recursive

Postby Kasuax » Thu Jun 30, 2016 2:01 pm

I got this to successfully work by doing the following.

  1. Make all my table ids integers
  2. Enabling qx::QxSqlDatabase::getSingleton()->setForceParentIdToAllChildren(true);
  3. Use qx::dao::save_with_relation_recursive(clone, qx::dao::save_mode::e_insert_only)
What are the tradeoffs with qx::QxSqlDatabase::getSingleton()->setForceParentIdToAllChildren(true);?
What does this actually do?
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: clone_with_relation_recursive

Postby qxorm » Thu Jun 30, 2016 3:27 pm

I got this to successfully work by doing the following.

Great !

What are the tradeoffs with qx::QxSqlDatabase::getSingleton()->setForceParentIdToAllChildren(true);?
What does this actually do?

When you enable this option, you can lose a bit in term of performance.
This option is used only by 1-n relationship : after saving the '1' side of the relationship, then it will update the 'n' side of the relationship : it iterates over all items to update the foreign key with the id of the parent item.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Open discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron