For storing hierarchical data in the database, Gallery 3 uses the ORM_MPTT subclass to ORM which implements Modified Preorder Tree Traversal. This is a technique for keeping track of parent/child relationships for a tree of data which allows for extremely efficient read access. There’s a great description of the concept and how it works on Sitepoint.com that you should read if you plan on working with MPTT. The ORM_MPTT class does the bulk of the work for you. Using it is very simple, simply add left_ptr, right_ptr, parent_id, and level integer columns to your database table and then have your model extend the ORM_MPTT class instead of ORM.