Breaking News
You are here : Home / Web development / CakePHP / cakePHP Tree Behavior :: Full Guide
Cake PHP Tree Behavior

cakePHP Tree Behavior :: Full Guide

CakePHP Tree behavior Full Guide:

CakePHP Tree Behavior is very Useful in creating a Hierarchical data.The CakePHP tree behavior is very efficient than Other Techniques.Tree Behavior can easily handle unlimited number if subcategories and very useful for multilevel menus .It is Based on MPPT Logic , But CakePHP handles every thing for you ,you dont have to completely understand this MPPT logic for using CakePHP Tree Behavior.
So today we got a Full Guide on cakePHP tree Behavior. Lets Get Moving and Understand the CakePHP tree Behavior.
[separator/]

CakePHP Tree Behavior Full Guide::Tutorial-

For Understanding the CakePHP Tree Behavior we’ll create a new cakePHP application ,which will have a database table named categories ,with Category Model ,Categories Controller and The view files (add.ctp ,edit.ctp , index.ctp) .So firstly lets see the Schema for the database which is required here.

cakePHP Tree Behavior Full Tutorial :: Database Schema-

Firslty we’ll create a new database for our new cakePHP and a new table inside that named ‘Categories’.IN the categories table we’ll need to have following Columns:

Column

Type Null Default
id int(10) No
parent_id int(10) Yes NULL
lft int(10) Yes NULL
rght int(10) Yes NULL
name varchar(255) Yes

The Above is the Table Structure for categories table.You can create it Or use the SQL below to create the table for you.

Now After Creating The Table .we’ll add some data to it.Run the query below in phpMyAdmin to add some data to your categories table.

Now that your table has data and now we set up our model.
[separator/]

cakePHP Tree Behavior Full Tutorial :: Model Setup-

Now Move To next step for understanding CakePHP Tree Behavior.and we set up Our MODEL for our tree behaviour . The line “acts as=array(‘Tree’)” Tells Category Model to use Tree Behavior.

That is all we need to do in the Our Model Now we setup our categories controller.Lets see the categories controller.
[separator/]

cakePHP Tree Behavior Full Guide :: Controller Setup-

Now We create the controller for our category model.Here You’ll see that controller covers all the action to demonstrate the cakePHP Tree Behavior.

[separator/]

cakePHP Tree Behavior Full Guide ::View Setup-

Now We create our view files for the controller actions we have created earlier.WE created three actions add,edit and index .so we gonna create index.ctp,add.ctp and edit .ctp .WE do not need to create views for the moveup , movedown, delete and removeNode Functions.

[jtabs]

Index.ctp ::

[jtab/]
add.ctp ::

[jtab/]
edit.ctp ::

[/jtabs]

After that You’ll be able to get CakePHP Tree Behavior demo application to work.Then You can easily Understand It.You’ll able to create,delete ,update tree items and more you’ll be able to move up move down and remove items from the tree.Now I’ll define which Function i have used here.

[separator/]

cakePHP Tree Behavior Full Tutorial ::Cake Tree Methods-

Here are Cake Tree methods used with the Description.

  • generatetreelist($conditions=null, $keyPath=null, $valuePath=null, $spacer= ‘_’, $recursive=null) – This is function used to print out the Tree Structure.
  • movedown() – Used to move down a node .
  • moveup() -Used to move up a node.
  • removeFromTree($id=null, $delete=false) – this function can perform two tasks,deleting and removing node from tree.
These are the important function used here.I have Included and Example which you can download from the Link below.
[separator/]

[jbutton icon="download" size="xlarge" link="http://download.designaeon.com/caketree.rar"]Download Example[/jbutton]

That was all ! Tnx for vising Design Aeon Web development Blog.Keep Vising.

[digg size="Compact"]

[pinterestfollow user="ramandeep000" /]

[google size="medium"/]

[separator/]

This Post Has Been Viewed 1,034 Times

Tags : , , , ,

9 Replies

  1. Kevin says:

    Hi Ramandeep:

    Thank you for you r help (time and expertise). Does this apply to CakePHP 1.3 only? I cannot get it to work with CakePHP 2.2. I’m wondering if there were some changes that I need to be aware of (e.g., $this->$html-> instead of just $html->, etc.

    If so, it would be wonderful of you to show the changes.

    Kevin

    1. Ramandeep Singh says:

      yeah its for cake1.3,,,There will be minor changes for 2.0 i guess

  2. Bettina Oberrauter says:

    Thanks for this great Tutorial, helps a lot!
    For the Cakephp 2.1 you just need to change the following commands:

    In the views:
    Change $html->link …. to $this->html->link….

    In the controller:
    Change generatetreelist to generateTreeList
    Change moveup() to moveUp()
    Change movedown() to moveDown()

    And it works great for CakePHP 2.1 )

    Regards,

    Bettina

  3. Livin' In China says:

    Thanks a lot Ramandeep!

    I was looking for this dynamic menu but there was nothing… until I found you!
    I’m trying to build a website using cakephp with a homemade CMS.
    That’s quite a task knowing that I’m just a beginner and I’m looking for concrete tutorials.

    However, I’d like to put an authorization with user name and password to complete your tutorial. I’d better get started then! :)

    Again, thanks for your work!

  4. Rasel says:

    Nice Post, so helpful post

  5. David says:

    Many thanks Ramandeep, helped me resolve a problem I had with static menus.

    As Bettina mentioned above, code works fine CakePHP 2.x once you update the Views and Controller.

  6. phamthuan says:

    thanks you, excellent tutorial.

  7. phamthuan says:

    If you use CakePHP 2.2, You must change: “$this->Html->” instead of “$html->”, and function “generateTreeList” instead of “generatetreelist”, I’m using cakephp 2.2.4, this is work great !

  8. David says:

    hello,
    is there the possibility to see the indentation of tree in index with pagination?

Leave a Reply

%d bloggers like this: