In the meeting before, my manager told me to study the nested category where in a category can be found another category. So I look inside the core helper of cake PHP (*see my previous post about my works) is there any helper I can use. Then I found the Tree helper. This is very handy to make some kind of nested things like that.
What we have to do is just add 3 columns with int type to save the tree structure information. Those three columns were parent_id (refer to the primary key of its parent), left and right (to save the next and previous node, I think). Once you have made this, don’t forget to configure the model and controller. I didn’t want to talk much about the code, because you can learn it by your self at cakePHP website, and it pretty easy to learn from there.
Data without parent_id will be treated as parent node and cake will seek the children where it has it primary key as parent_id on it. The parent_id is the only field that we must assign the value by ourselves and for the left and right column it will be automatically being populated by the cake.
It was so simple to manipulate and retrieve data from the tree. To alter the parent, we just to assign new value to parent_id and it done. Same also for the addition data, just give the value on parent_id. But be careful, when you delete a node, cake will also automatically delete its entire child. In my application, before deletion taking place, I assign the child’s parent_id to null in order to prevent the children being deleted.
Cake also provides you some function to get information of entire child of a node, including the children of its children. And there is also function to get the entire parent, including the parent’s parent. I used to manage tree of data in database too for my thesis. And this kind operation needs to be recursive and querying several times. But cake makes that looks easy. I’m pretty sure, that behind the cake function it also has the same complexity.
When you change or add parent information, we must extra careful because we can accidentally make a loop. For my thesis I used some kind of hand made complex function just to check is there any loop would happen if I alter / add the data. Just for preventing my application trapped in infinity loop in the future. In the cake, when I try to make a loop. It automagically deny to save. Quite smart framework didn’t it?... :D
The cake can also able print the tree structure but I admit that it is very ugly. Cake use spaces by default. But I successfully modify it using some kind of plugin, instead of spacer, it will use tag html for list. With little modification like that, now you can combine it with JQuery to make the tree looks more interactive.
you can click on the title and you will get redirected to cake php website
What we have to do is just add 3 columns with int type to save the tree structure information. Those three columns were parent_id (refer to the primary key of its parent), left and right (to save the next and previous node, I think). Once you have made this, don’t forget to configure the model and controller. I didn’t want to talk much about the code, because you can learn it by your self at cakePHP website, and it pretty easy to learn from there.
Data without parent_id will be treated as parent node and cake will seek the children where it has it primary key as parent_id on it. The parent_id is the only field that we must assign the value by ourselves and for the left and right column it will be automatically being populated by the cake.
It was so simple to manipulate and retrieve data from the tree. To alter the parent, we just to assign new value to parent_id and it done. Same also for the addition data, just give the value on parent_id. But be careful, when you delete a node, cake will also automatically delete its entire child. In my application, before deletion taking place, I assign the child’s parent_id to null in order to prevent the children being deleted.
Cake also provides you some function to get information of entire child of a node, including the children of its children. And there is also function to get the entire parent, including the parent’s parent. I used to manage tree of data in database too for my thesis. And this kind operation needs to be recursive and querying several times. But cake makes that looks easy. I’m pretty sure, that behind the cake function it also has the same complexity.
When you change or add parent information, we must extra careful because we can accidentally make a loop. For my thesis I used some kind of hand made complex function just to check is there any loop would happen if I alter / add the data. Just for preventing my application trapped in infinity loop in the future. In the cake, when I try to make a loop. It automagically deny to save. Quite smart framework didn’t it?... :D
The cake can also able print the tree structure but I admit that it is very ugly. Cake use spaces by default. But I successfully modify it using some kind of plugin, instead of spacer, it will use tag html for list. With little modification like that, now you can combine it with JQuery to make the tree looks more interactive.
you can click on the title and you will get redirected to cake php website
I, of course, a newcomer to this blog, but the author does not agree
ReplyDeletedoesn't agree on what?...
ReplyDelete