Skip to content

Commit

Permalink
Make getLevel() accept an entity.
Browse files Browse the repository at this point in the history
This makes all the methods in TreeBehavior consistent in that they all
accept an entity.
  • Loading branch information
markstory committed Jan 30, 2015
1 parent 3548dda commit 439648a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/ORM/Behavior/TreeBehavior.php
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/ORM/Behavior/TreeBehaviorTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit 439648a

Please sign in to comment.