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
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.