Browse Source

Make getLevel() accept an entity.

This makes all the methods in TreeBehavior consistent in that they all
accept an entity.
Mark Story 11 years ago
parent
commit
439648ad4f

+ 8 - 3
src/ORM/Behavior/TreeBehavior.php

@@ -797,15 +797,20 @@ class TreeBehavior extends Behavior
 /**
  * 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) {

+ 5 - 1
tests/TestCase/ORM/Behavior/TreeBehaviorTest.php

@@ -864,7 +864,11 @@ class TreeBehaviorTest extends TestCase
      */
     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);