Page 1 of 1

Best way to work with hierarchical data

PostPosted: Mon Mar 26, 2012 7:28 pm
by Davita
Hi :)
I wanted to know if QxOrm supports hierarchical data, for example, if a row has a reference of another row in same table. How should I do mapping, how should I query, anything I should consider when working with hierarchical data would be welcome :)

Thanks

Re: Best way to work with hierarchical data

PostPosted: Mon Mar 26, 2012 8:40 pm
by Davita
I think I found out how to do that. I updated blog class (from official samples) as shown below

Code: Select all
namespace qx {
template <> void register_class(QxClass<blog> & t)
{
   t.id(& blog::m_id, "blog_id");

   t.data(& blog::m_text, "blog_text");
   t.data(& blog::m_dt_creation, "date_creation");

[b]   t.relationManyToOne(& blog::m_parent, "parent_id");
   t.relationOneToMany(& blog::m_children, "list_blog", "parent_id");
[/b]   t.relationManyToOne(& blog::m_author, "author_id");
   t.relationOneToMany(& blog::m_commentX, "list_comment", "blog_id");
   t.relationManyToMany(& blog::m_categoryX, "list_category", "category_blog", "blog_id", "category_id");
}}


Till now, it works. Is this a recommended approach or does it have any drawbacks? Can I use this solution in production?

Thanks :)

Re: Best way to work with hierarchical data

PostPosted: Tue Mar 27, 2012 7:49 am
by QxOrm admin
Hi,

Till now, it works. Is this a recommended approach or does it have any drawbacks? Can I use this solution in production?

Yes, it should work without problem !

I just recommend to use smart-pointers for your blog::m_parent and blog::m_children C++ properties.
For example, with boost smart-pointers (you could use Qt smart-pointers if you prefer) :
Code: Select all
boost::shared_ptr<blog> m_parent;
QList< boost::shared_ptr<blog> > m_children;

Re: Best way to work with hierarchical data

PostPosted: Tue Mar 27, 2012 7:55 am
by Davita
Thank you very much :)