Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP tree behaviour

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 513
    Comment on it

    In a tree structure data or records are stored in hierarchical manner, In tree structure the starting data record  is called root node or parent node and other are child or subchild of parent node.

    For implementing tree behaviour in cakephp , First we have to create table in our database for storing our tree. we are creating simple table name categories.

    Our table should contain column naming id,parent_id,left,right,name.

    Below query will create table in your database.

    CREATE TABLE categories (
        id INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        parent_id INTEGER(10) DEFAULT NULL,
        left INTEGER(10) DEFAULT NULL,
        right INTEGER(10) DEFAULT NULL,
        name VARCHAR(255) DEFAULT '',
        PRIMARY KEY  (id)
    );
    

    Then we have to insert data into our table by using below queries:-

    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(1, 'My Categories', NULL, 1, 30);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(2, 'Fun', 1, 2, 15);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(3, 'Sport', 2, 3, 8);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(4, 'Surfing', 3, 4, 5);
    INSERT INTO `categories` (`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(5, 'Extreme knitting', 3, 6, 7);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(6, 'Friends', 2, 9, 14);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(7, 'Gerald', 6, 10, 11);
    INSERT INTO `categories`(`id`, `name`,`parent_id`,`left`,`right`) 
    VALUES(8, 'Gwendolyn', 6, 12, 13);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(9, 'Work', 1, 16, 29);
    INSERT INTO `categories`(`id`,`name`,`parent_id`,`left`,`right`) 
    VALUES(10, 'Reports', 9, 17, 22);

    Then we have to create model in app/model with name Category.php .

    Put below code into your category.php file.

    <?php
    class Category extends AppModel {
        var $name = 'Category';
        var $actsAs = array('Tree');
    }
    ?>

    Then we have to create view naming add.ctp & index.ctp. for adding more categories and displaying them.

    index.ctp contain following code:-

    <?php
    echo "<table>";
    foreach($categorylist as $key=>$value){
    echo "<tr>";
    echo "<td>";
    echo $value;
    echo "</td>";
    echo "</tr>";
    }
    echo "</table>";
    echo $this->Html->link("Add Category",array('action'=>'add'));
    ?>

    add.ctp contain following code:-

    <?php
    
    
    echo $this->Html->link('Back',array('action'=>'index'));
    
    echo $this->Form->create('Category');
    echo $this->Form->input('name',array('label'=>'Name'));
    echo $this->Form->input('parent_id',array('label'=>'Parent'));
    echo $this->Form->end('Add');
    
    
    ?>

    Then we have to create controller file naming CategoryController.php in app/controller, and provide function for our add and index.ctp.

    Below is the logic for the controller.

    <?php 
    class CategoriesController extends AppController {
    
        var $name = 'Categories';
    
    	function index() {
    		$this->data = $this->Category->generateTreeList(null, null, null, '&nbsp;&nbsp;&nbsp;');
    		$this->set('categorylist',$this->data);
        }
    
        public function add() {
        			
        			if (!empty($this->data)) {
    					$this->Category->save($this->data);
    					$this->redirect(array('action'=>'index'));
    				} 
    				else {
    					$parents[0] = "[ SELECT PARENT ]";
    					$categorylist = $this->Category->generateTreeList(null,null,null," ");
    				if($categorylist) {
    					foreach ($categorylist as $key=>$value)
    					$parents[$key] = $value;
    				}
    					$this->set(compact('parents'));
    				}
    				
    
    
        }
    }
    ?>

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: