# NestedSetsQueryBehavior

> Adds root and leaf queries to an yii\db\ActiveQuery whose model is managed by yii2\extensions\nestedsets\NestedSetsBehavior.

[API reference](/docs/api/index.md) › [Nested sets behavior](/docs/extensions/nested-sets-behavior/reference/index.md) › NestedSetsQueryBehavior

| class | `yii2\extensions\nestedsets\NestedSetsQueryBehavior` |
| --- | --- |
| Extends | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/NestedSetsQueryBehavior.php`](https://github.com/yii2-extensions/nested-sets-behavior/blob/main/src/NestedSetsQueryBehavior.php#L19) |

Adds root and leaf queries to an [`ActiveQuery`](/docs/api/yii/db/activequery/index.md) whose model is managed by [`NestedSetsBehavior`](/docs/extensions/nested-sets-behavior/reference/nestedsetsbehavior/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-owner"></span>[`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) | `yii\base\T\|null` | The owner of this behavior Default: `null`. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-attach"></span>[`attach()`](/docs/api/yii/base/behavior/index.md#method-attach) | Attaches the behavior object to the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detach"></span>[`detach()`](/docs/api/yii/base/behavior/index.md#method-detach) | Detaches the behavior object from the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-events"></span>[`events()`](/docs/api/yii/base/behavior/index.md#method-events) | Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`leaves()`](#method-leaves) | Restricts the owner query to leaf nodes, ordered by tree identifier when enabled and then by left value. | `NestedSetsQueryBehavior` |
| [`roots()`](#method-roots) | Restricts the owner query to root nodes, ordered by tree identifier when enabled and then by left value. | `NestedSetsQueryBehavior` |

## Method details

### leaves() <span id="method-leaves"></span>

```php
public function leaves(): yii2\extensions\nestedsets\T
```

Restricts the owner query to leaf nodes, ordered by tree identifier when enabled and then by left value.

**Returns**

`yii2\extensions\nestedsets\T`: Owner query with the leaf condition and ordering applied.

**Throws**

- [`LogicException`](https://www.php.net/manual/en/class.logicexception.php): if the behavior is not attached or the query model is not an [`ActiveRecord`](/docs/api/yii/db/activerecord/index.md).

### roots() <span id="method-roots"></span>

```php
public function roots(): yii2\extensions\nestedsets\T
```

Restricts the owner query to root nodes, ordered by tree identifier when enabled and then by left value.

**Returns**

`yii2\extensions\nestedsets\T`: Owner query with the root condition and ordering applied.

**Throws**

- [`LogicException`](https://www.php.net/manual/en/class.logicexception.php): if the behavior is not attached or the query model is not an [`ActiveRecord`](/docs/api/yii/db/activerecord/index.md).