« Kohana ORM Tip | Kohana 2.3 look ahead! »

Kohana ORM Tip : ORM Tree

Well the last ORM tip seemed to get a bit of traffic so I thought I would throw another one together. This time it covers the little known ORM_Tree library. This is (IMO) a spectacular example of the flexibility and power of Kohana, by taking a look at this ORM extension (system/libraries/ORM_Tree) you can see quite how easily this age old storage pattern is solved by extending the ORM class.

Using it is even simpler!

Our Scenario

We want to implement a standard categories system in our site but not have to go through all the trouble of building it from scratch and would love to harness the existing power of the ORM library . .enter ORM_Tree

The table

CREATE TABLE `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL DEFAULT '',
  `parent_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`id`)
)

The Model

class Category_Model extends ORM_Tree
{
	protected $children = "categories";
}

Using it

To test this let’s add some test data;

  INSERT INTO `categories` VALUES (1,'root',0),(2,'beer',1),(3,'meat',1),(4,'pork',3),(5,'beef',3);

So after adding some appropriate information using it is as simple as..

  print_r(ORM::factory("Category", 1)->children->as_array());

…to return the children for the category with id 1

  echo ORM::factory("category", 4)->parent->name;

..would print the parent name of the category with ID 4

Hopefully that little tip will help someone! Any questions then please don’t hesitate to ask!

/Matt

Comments

  1. Zeelot | September 26th, 2008 | 5:24 am

    nice tutorial but can you show more usage in php rather than SQL commands? how do I add a few subcategories to a category? how do I change a categories parent?

  2. spirit | September 30th, 2008 | 9:20 pm

    Is it possible to get the path (like a breadcrumb) to a child (I mean all its parents)?

  3. Matt | September 30th, 2008 | 10:45 pm

    Thanks for the comments!

    Zeelot - I’ll try and get some more usage examples of this together for you, although the beauty of this is that it is a simple extension of the ORM library, so it inherits all the standard ORM features

    Spirit - Off the top of my head you could always retreive the child and then recursively determine the parent until you encounter the root node

      while(true)
      {
        if($node->parent == null) break;
     
        $crumbs[] = $node->parent;
        $node = $node->parent;
      }

    **Note the above is sudo code, but that is the basic idea, just start with your node in question and loop through its parents until you hit root

    Hope that helps!

    /Matt

  4. jshaw86 | October 16th, 2008 | 10:29 pm

    I’ve been trying to get this to work for a couple hours now. I had a couple questions:
    1) do we still need our pivot table relationship arrays(ie has_one, has_and_belongs_to_many).
    2) I don’t get the children thing, why is categories a child of Category.

    My situtation is I have 3 entities:events,people,sororities, all are many-to-many relationships. So all entities are parents and children of eachother. Thanks.

  5. Matt | October 18th, 2008 | 10:32 am

    Hi jshaw,

    I noticed shortly after your post here that Wouter helped you out with this over at the Kohana Forums, so hopefully you have managed to resolve this!

    For others wanting to follow the resolution to this then check out the thread at the forums.

    /Matt

Post a comment