Skip to content

Commit 5a5403e

Browse files
committed
Refactor NestedSet
1 parent c3a67cc commit 5a5403e

File tree

3 files changed

+33
-53
lines changed

3 files changed

+33
-53
lines changed

CHANGELOG.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
data (lft and rgt values).
77
* The root node is not required now. You can use `saveAsRoot` or `makeRoot` method.
88
New model is saved as root by default.
9-
* You can now create as many nodes and in any order as possible within single
9+
* You can now create as many nodes and in any order as you want within single
1010
request.
1111
* Laravel 2 is supported but not required.
1212
* `ancestorsOf` now doesn't include target node into results.
@@ -15,6 +15,8 @@
1515
* Default order is not applied by default.
1616
* New method `descendantsOf` that allows to get descendants by id of the node.
1717
* Added `countErrors` and `isBroken` methods to check whether the tree is broken.
18+
* `NestedSet::createRoot` has been removed.
19+
* `NestedSet::column` doesn't create a foreign key anymore.
1820

1921
### 1.1.0
2022

UPGRADE.markdown

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
### Upgrading to 1.2
1+
### Upgrading to 2.0
22

33
Calling `$parent->append($node)` and `$parent->prepend($node)` now automatically
44
saves `$node`. Those functions returns whether the node was saved.
55

6-
`ancestorsOf` now return ancestors only not including target node.
6+
`ancestorsOf` now return ancestors only, not including target node.
77

8-
Default order is not applied automatically.
8+
Default order is not applied automatically, so if you need nodes to be in tree-order
9+
you should call `defaultOrder` on the query.
10+
11+
Since root node is not required now, `NestedSet::createRoot` method has been removed.
12+
13+
`NestedSet::columns` now doesn't create a foreign key for a `parent_id` column.

src/Kalnoy/Nestedset/NestedSet.php

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,48 @@
1-
<?php namespace Kalnoy\Nestedset;
1+
<?php
22

3-
use \Illuminate\Database\Connection;
4-
use \Illuminate\Database\Schema\Blueprint;
3+
namespace Kalnoy\Nestedset;
4+
5+
use Illuminate\Database\Connection;
6+
use Illuminate\Database\Schema\Blueprint;
57

68
class NestedSet {
79

810
/**
9-
* Add NestedSet columns to the table. Also create index and foreign key.
10-
*
11-
* @param Blueprint $table
11+
* Add default nested set columns to the table. Also create an index.
1212
*
13-
* @return void
13+
* @param \Illuminate\Database\Schema\Blueprint $table
14+
* @param string $primaryKey
1415
*/
15-
static public function columns(Blueprint $table, $primaryKey = 'id')
16+
public static function columns(Blueprint $table, $primaryKey = 'id')
1617
{
1718
$table->integer(Node::LFT);
1819
$table->integer(Node::RGT);
1920
$table->unsignedInteger(Node::PARENT_ID)->nullable();
2021

21-
$table->index(array(Node::LFT, Node::RGT, Node::PARENT_ID), 'nested_set_index');
22-
23-
$table
24-
->foreign(Node::PARENT_ID, self::getForeignKeyName($table->getTable()))
25-
->references($primaryKey)
26-
->on($table->getTable())
27-
->onDelete('cascade');
28-
}
29-
30-
/**
31-
* Get foreign key name for the table.
32-
*
33-
* @param string $table
34-
*
35-
* @return string
36-
*/
37-
protected static function getForeignKeyName($table)
38-
{
39-
return $table.'_nested_set_foreign';
22+
$table->index(self::getDefaultColumns());
4023
}
4124

4225
/**
4326
* Drop NestedSet columns.
4427
*
45-
* @param Blueprint $table
46-
*
47-
* @return void
28+
* @param \Illuminate\Database\Schema\Blueprint $table
4829
*/
49-
static public function dropColumns(Blueprint $table)
30+
public static function dropColumns(Blueprint $table)
5031
{
51-
$table->dropForeign('nested_set_foreign');
52-
$table->dropIndex('nested_set_index');
53-
$table->dropColumn(Node::LFT, Node::RGT, Node::PARENT_ID);
32+
$columns = self::getDefaultColumns();
33+
34+
$table->dropIndex($columns);
35+
$table->dropColumn($columns);
5436
}
5537

5638
/**
57-
* Create root node.
58-
*
59-
* @param string $table
60-
* @param array $extra
61-
* @param string $connection
62-
*
63-
* @return boolean
39+
* Get a list of default columns.
40+
*
41+
* @return array
6442
*/
65-
static function createRoot($table, array $extra = array(), $connection = null)
43+
public static function getDefaultColumns()
6644
{
67-
$extra = array_merge($extra, array(
68-
Node::LFT => 1,
69-
Node::RGT => 2,
70-
Node::PARENT_ID => NULL,
71-
));
72-
73-
return \DB::connection($connection)->table($table)->insert($extra);
45+
return [ Node::LFT, Node::RGT, Node::PARENT_ID ];
7446
}
47+
7548
}

0 commit comments

Comments
 (0)