PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] How to display treeview in view using cakephp 2

 March 11, 2022     cakephp, cakephp-2.0, php     No comments   

Issue

enter image description hereHi guys i have problem to display treeview display in cakephp 2...using same table as parents and child as relationship.

Table MediaDirectory

id Title Parent_id
1 Test 1 null
2 Test 2 null
3 Test 3 1
4 Test 4 1
5 Test 5 3
6 Test 6 2
7 Test 7 2

ContentsController.php

public function index()
{
    $Treeview = $this->MediaDirectory->find("all", 
        array(
            'conditions' => array(
                'MediaDirectory.parent_id' => null
            )
        )
    );
    
    $this->set(compact('$Treeview'));
}

Solution

First I don't understand how can you have 2 times a field named parent_id in your table. You should remove the one which is null. You need 3 differents fields in your table :

  • parent_id to store the id of the parent object.
  • lft to store the lft value of the current row.
  • rght to store the rght value of the current row.

Then you need to add the following line in your model :

public $actsAs = array('Tree');

If you want that it to work, you have to insert your data using your controller. lft and rght are automatically defined by the core when you save some data in it.

For instance, in your controller

$data['MediaDirectory']['parent_id'] = 10;
$data['MediaDirectory']['title'] = 'Test 1';
$this->MediaDirectory->save($data);

And after that you should replace your code in your controller like this :

public function index() {
  $Treeview = $this->MediaDirectory->find("threaded", 
    array(
      'conditions' => array(
        'MediaDirectory.parent_id'=> null,
        'MediaDirectory.fk_id'=> $cid // Not sure what this is suppose to do
      )
    )
  );
  $this->set(compact('Treeview')); // TreeviewChild name was wrong
}

The query build find("threaded") is suppose to return tree data. Do not hesitate to use debug() function to visualize what data you are currently manipulating.

Everything you need is there about the version 2.x : https://book.cakephp.org/2/en/core-libraries/behaviors/tree.html



Answered By - Arthur .B
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing