Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to find root from node in TreeBehavior #12539

Closed
1 of 2 tasks
Rahul-Bhagwat opened this issue Sep 5, 2018 · 8 comments
Closed
1 of 2 tasks

Unable to find root from node in TreeBehavior #12539

Rahul-Bhagwat opened this issue Sep 5, 2018 · 8 comments

Comments

@Rahul-Bhagwat
Copy link

This is a (multiple allowed):

  • bug

  • [x ] enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.4.

  • Platform and Target: Xampp with MySQL

What you did

I tried to find the root of the TREE when a child node is given.

What happened

I didn't find any function supporting this feature.

What you expected to happen

As CakePHP have many function to find out list of children from the parent node(ROOT). I was expecting to get the ROOT when children node was provided.

There could be function to extract the path to the node with query like:
SELECT title FROM tree WHERE lft < 4 AND rgt > 5 ORDER BY lft ASC;
and the return the root from it when child node is provided.

P.S. Remember, an issue is not the place to ask questions. You can use Stack Overflow
for that or join the #cakephp channel on irc.freenode.net, where we will be more
than happy to help answer your questions.

Before you open an issue, please check if a similar issue already exists or has been closed before.

@Rahul-Bhagwat
Copy link
Author

Rahul-Bhagwat commented Sep 5, 2018

In order to find root node I used the following

             $childNode = $this->FlauntsComments->find('all', array(
                  'conditions' => array('id' => $id)
              ))->first();
              $root =   $this->FlauntsComments->find('threaded', array(
                'conditions' => array(
                    'FlauntsComments.lft <=' => $childNode['lft'],
                    'FlauntsComments.rght >=' => $childNode['rght']
                )
              ))->toArray();

                $this->log('Parent id__ '.$root[0]['id']);

Where $id is the id of the child node. And $root[0]['id'] gives the id of the root.

@Rahul-Bhagwat Rahul-Bhagwat changed the title Unable to find root from node Unable to find root from node in TreeBehavior Sep 5, 2018
@dakota
Copy link
Member

dakota commented Sep 5, 2018

This already exists in the form of the path finder.

From the book

$nodeId = 5;
$crumbs = $categories->find('path', ['for' => $nodeId]);

@Rahul-Bhagwat
Copy link
Author

Thanks @dakota

@saeideng
Copy link
Member

saeideng commented Sep 5, 2018

if you need to parent of a node ( direct parent )

$categories->find()->where(['id'=>$childNode->parent_id]);

but for tree of a node as @dakota said

$nodeId = 5;
$crumbs = $categories->find('path', ['for' => $nodeId]);

can you explain more , what are you expecting ?

@Rahul-Bhagwat
Copy link
Author

Rahul-Bhagwat commented Sep 6, 2018

Hi @saeideng

screenshot 3

Above is a sample from my database. As you can see, all the comments are from the same flaunts.

ID: 1855 is the main comment on the flaunt whereas ID: 1856 is the reply to the main comment(1855) and ID: 1857 is the reply to comment(1856).

These table have the TREE behavior creating left tree and right tree.

What I expect is if I Get the child node ID: 1862, I can directly get the root node ID: 1855.
Is there any defined function to achieve so?

Right now I am using:
$crumbs = $this->FlauntsComments->find('path', ['for' => $childNode])->first(); $this->log($crumbs['id']);
$crumbs['id'] returns the root node ID.

@saeideng
Copy link
Member

saeideng commented Sep 6, 2018

I think your last code is what you are expecting , true ?

$crumbs = $this->FlauntsComments->find('path', ['for' => $childNode])->first();

can we close this issue now ?

@saeideng
Copy link
Member

saeideng commented Sep 6, 2018

also

return $this->_scope($query)
->where([
"$left <=" => $node->get($config['left']),
"$right >=" => $node->get($config['right']),
])

is like to your code in above

'conditions' => array(
                    'FlauntsComments.lft <=' => $childNode['lft'],
                    'FlauntsComments.rght >=' => $childNode['rght']
                )

and I think find('pat ... solved your need , so closing this issue

@saeideng saeideng closed this as completed Sep 6, 2018
@Rahul-Bhagwat
Copy link
Author

@saeideng @dakota
It worked for me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants