Completed
Branch master (fd1517)
by
unknown
55:42
created

NestedSetQueryBehavior   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 1
lcom 1
cbo 3
dl 0
loc 63
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A roots() 0 7 1
1
<?php
2
3
/**
4
 *
5
 * @link https://github.com/creocoder/yii2-nested-set-behavior
6
 * @copyright Copyright (c) 2013 Alexander Kochetov
7
 * @license http://opensource.org/licenses/BSD-3-Clause
8
 */
9
namespace fangface\behaviors;
10
11
use fangface\db\ActiveRecord;
12
use yii\base\Behavior;
13
use yii\db\ActiveQuery;
14
15
/**
16
 *
17
 * @author Alexander Kochetov <[email protected]>
18
 */
19
class NestedSetQueryBehavior extends Behavior
20
{
21
22
    /**
23
     * @var ActiveQuery the owner of this behavior.
24
     */
25
    public $owner;
26
27
    /**
28
     * @var boolean
29
     */
30
    public $hasManyRoots = false;
31
32
    /**
33
     * @var boolean
34
     */
35
    public $hasPaths = false;
36
37
    /**
38
     * @var string
39
     */
40
    public $rootAttribute = 'root';
41
42
    /**
43
     * @var string
44
     */
45
    public $leftAttribute = 'lft';
46
47
    /**
48
     * @var string
49
     */
50
    public $rightAttribute = 'rgt';
51
52
    /**
53
     * @var string
54
     */
55
    public $levelAttribute = 'level';
56
57
    /**
58
     * @var string
59
     */
60
    public $nameAttribute = 'name';
61
62
    /**
63
     * @var string
64
     */
65
    public $pathAttribute = 'path';
66
67
68
    /**
69
     * Gets root node(s)
70
     *
71
     * @return \yii\db\ActiveQuery the owner
72
     */
73
    public function roots()
74
    {
75
        /** @var $modelClass ActiveRecord */
76
        $modelClass = $this->owner->modelClass;
77
        $this->owner->andWhere($modelClass::getDb()->quoteColumnName($this->leftAttribute) . '=1');
78
        return $this->owner;
79
    }
80
81
}
82