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
Welcome to ninjapenguin. Personal blog of Matthew Wells. I'm a web developer from the North West and enjoy working with Kohana, Mootools and Git
Comments
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?
Is it possible to get the path (like a breadcrumb) to a child (I mean all its parents)?
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
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.
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
How can I add categories/subcategories in table ‘categories’ using ORM or I need every time using SQL query like this:
INSERT INTO `categories` VALUES (1,’root’,0),(2,’beer’,1),(3,’meat’,1),(4,’pork’,3),(5,’beef’,3); ???
i have tried a really simple example of this and can’t get it working.
do i need to have a foreign key on the table ?
this is my table
catalogues
id
parent_id
name
my model file
class Catalogue_Model extends ORM_Tree {
protected $children = “catalogues”;
}
my code
$catalogues = ORM::factory(‘catalogue’)->where(‘parent_id’, $id)->find_all();
foreach($catalogues as $cat) {
foreach ($cat->children as $subcat) {
do stuff
}
error message
Fatal error: Class ‘_Model’ not found in \system\libraries\ORM.php on line 82
Hi: i receive the same error like bumperbox, anyone have the solution?
@bumperbox & Bicho44
Use protected $ORM_Tree_children instead of protected $children.
GreetZ
Great tutorial that was a big help to me, thanks a lot!
Post a comment