From 439648ad4f3129994edcedbb949817aafc1b90e7 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 29 Jan 2015 22:02:08 -0500 Subject: [PATCH] Make getLevel() accept an entity. This makes all the methods in TreeBehavior consistent in that they all accept an entity. --- src/ORM/Behavior/TreeBehavior.php | 11 ++++++++--- tests/TestCase/ORM/Behavior/TreeBehaviorTest.php | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ORM/Behavior/TreeBehavior.php b/src/ORM/Behavior/TreeBehavior.php index 15e8ce4fd75..edb3979b602 100644 --- a/src/ORM/Behavior/TreeBehavior.php +++ b/src/ORM/Behavior/TreeBehavior.php @@ -797,15 +797,20 @@ protected function _getPrimaryKey() /** * Returns the depth level of a node in the tree. * - * @param int|string $id Primary key of the node. + * @param int|string|\Cake\Datasource\EntityInterface $entity The entity or primary key get the level of. * @return int|bool Integer of the level or false if the node does not exist. */ - public function getLevel($id) + public function getLevel($entity) { + $primaryKey = $this->_getPrimaryKey(); + $id = $entity; + if (!is_scalar($entity)) { + $id = $entity->get($primaryKey); + } $config = $this->config(); $entity = $this->_table->find('all') ->select([$config['left'], $config['right']]) - ->where([$this->_getPrimaryKey() => $id]) + ->where([$primaryKey => $id]) ->first(); if ($entity === null) { diff --git a/tests/TestCase/ORM/Behavior/TreeBehaviorTest.php b/tests/TestCase/ORM/Behavior/TreeBehaviorTest.php index f028eded1b3..74defa82f28 100644 --- a/tests/TestCase/ORM/Behavior/TreeBehaviorTest.php +++ b/tests/TestCase/ORM/Behavior/TreeBehaviorTest.php @@ -864,7 +864,11 @@ public function testFindPathWithAssociation() */ public function testGetLevel() { - $result = $this->table->getLevel(8); + $entity = $this->table->get(8); + $result = $this->table->getLevel($entity); + $this->assertEquals(3, $result); + + $result = $this->table->getLevel($entity->id); $this->assertEquals(3, $result); $result = $this->table->getLevel(5);