Download the PHP package djvibegga/yii2-nested-set-behavior without Composer
On this page you can find all versions of the php package djvibegga/yii2-nested-set-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download djvibegga/yii2-nested-set-behavior
More information about djvibegga/yii2-nested-set-behavior
Files in djvibegga/yii2-nested-set-behavior
Package yii2-nested-set-behavior
Short Description This extension allows you to get functional for nested set trees.
License BSD-3-Clause
Homepage https://github.com/djvibegga/yii2-nested-set-behavior
Informations about the package yii2-nested-set-behavior
Nested Set behavior for Yii 2
This extension allows you to get functional for nested set trees.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Configuring
First you need to configure model as follows:
Second you need to configure query model as follows:
There is no need to validate fields specified in leftAttribute
,
rightAttribute
, rootAttribute
and levelAttribute
options. Moreover,
there could be problems if there are validation rules for these. Please
check if there are no rules for fields mentioned in model's rules() method.
In case of storing a single tree per database, DB structure can be built with
schema/schema.sql
. If you're going to store multiple trees you'll need
schema/schema-many-roots.sql
.
By default leftAttribute
, rightAttribute
and levelAttribute
values are
matching field names in default DB schemas so you can skip configuring these.
There are two ways this behavior can work: one tree per table and multiple trees
per table. The mode is selected based on the value of hasManyRoots
option that
is false
by default meaning single tree mode. In multiple trees mode you can
set rootAttribute
option to match existing field in the table storing the tree.
Selecting from a tree
In the following we'll use an example model Category
with the following in its
DB:
- 1. Mobile phones
- 2. iPhone
- 3. Samsung
- 4. X100
- 5. C200
- 6. Motorola
- 7. Cars
- 8. Audi
- 9. Ford
- 10. Mercedes
In this example we have two trees. Tree roots are ones with ID=1 and ID=7.
Getting all roots
Using NestedSet::roots()
:
Result:
Array of Active Record objects corresponding to Mobile phones and Cars nodes.
Getting all descendants of a node
Using NestedSet::descendants()
:
Result:
Array of Active Record objects corresponding to iPhone, Samsung, X100, C200 and Motorola.
Getting all children of a node
Using NestedSet::children()
:
Result:
Array of Active Record objects corresponding to iPhone, Samsung and Motorola.
Getting all ancestors of a node
Using NestedSet::ancestors()
:
Result:
Array of Active Record objects corresponding to Samsung and Mobile phones.
Getting parent of a node
Using NestedSet::parent()
:
Result:
Array of Active Record objects corresponding to Cars.
Getting node siblings
Using NestedSet::prev()
or
NestedSet::next()
:
Result:
Array of Active Record objects corresponding to Mercedes.
Getting the whole tree
You can get the whole tree using standard AR methods like the following.
For single tree per table:
For multiple trees per table:
Modifying a tree
In this section we'll build a tree like the one used in the previous section.
Creating root nodes
You can create a root node using NestedSet::saveNode()
.
In a single tree per table mode you can create only one root node. If you'll attempt
to create more there will be CException thrown.
Result:
- 1. Mobile Phones
- 2. Cars
Adding child nodes
There are multiple methods allowing you adding child nodes. To get more info about these refer to API. Let's use these to add nodes to the tree we have:
Result:
- 1. Mobile phones
- 3. Audi
- 4. Ford
- 5. Mercedes
- 2. Cars
Logically the tree above doesn't looks correct. We'll fix it later.
Result:
- 1. Mobile phones
- 3. Audi
- 4. Ford
- 5. Mercedes
- 2. Cars
- 6. iPhone
- 7. Samsung
- 8. Motorola
Result:
- 1. Mobile phones
- 3. Audi
- 9. С200
- 10. X100
- 4. Ford
- 5. Mercedes
- 2. Cars
- 6. iPhone
- 7. Samsung
- 8. Motorola
Modifying a tree
In this section we'll finally make our tree logical.
Tree modification methods
There are several methods allowing you to modify a tree. To get more info about these refer to API.
Let's start:
Result:
- 1. Mobile phones
- 6. iPhone
- 7. Samsung
- 10. X100
- 9. С200
- 8. Motorola
- 2. Cars
- 3. Audi
- 4. Ford
- 5. Mercedes
Moving a node making it a new root
There is a special moveAsRoot()
method that allows moving a node and making it
a new root. All descendants are moved as well in this case.
Example:
Identifying node type
There are three methods to get node type: isRoot()
, isLeaf()
, isDescendantOf()
.
Example: