Passed
Push — master ( 344c14...9aa841 )
by Christopher
01:46
created

src/ActiveQuery.php (2 issues)

1
<?php
2
/**
3
 * @link      https://github.com/chrmorandi/yii2-ldap for the source repository
4
 * @package   yii2-ldap
5
 * @author    Christopher Mota <[email protected]>
6
 * @license   MIT License - view the LICENSE file that was distributed with this source code.
7
 * @since     1.0.0
8
 */
9
10
namespace chrmorandi\ldap;
11
12
use chrmorandi\ldap\ActiveRecord;
13
use chrmorandi\ldap\Connection;
14
use yii\db\ActiveQueryInterface;
15
use yii\db\ActiveQueryTrait;
16
use yii\db\ActiveRelationTrait;
17
18
/**
19
 * ActiveQuery represents a DB query associated with an Active Record class.
20
 *
21
 * An ActiveQuery can be a normal query or be used in a relational context.
22
 *
23
 * ActiveQuery instances are usually created by [[ActiveRecord::find()]].
24
 * Relational queries are created by [[ActiveRecord::hasOne()]] and [[ActiveRecord::hasMany()]].
25
 *
26
 * Normal Query
27
 * ------------
28
 *
29
 * ActiveQuery mainly provides the following methods to retrieve the query results:
30
 *
31
 * - [[one()]]: returns a single record populated with the first row of data.
32
 * - [[all()]]: returns all records based on the query results.
33
 * - [[count()]]: returns the number of records.
34
 * - [[min()]]: returns the min over the specified column.
35
 * - [[max()]]: returns the max over the specified column.
36
 * - [[exists()]]: returns a value indicating whether the query result has data or not.
37
 *
38
 * Because ActiveQuery extends from [[Query]], one can use query methods, such as [[where()]],
39
 * [[orderBy()]] to customize the query options.
40
 *
41
 * ActiveQuery also provides the following additional query options:
42
 *
43
 * - [[with()]]: list of relations that this query should be performed with.
44
 * - [[joinWith()]]: reuse a relation query definition to add a join to a query.
45
 * - [[indexBy()]]: the name of the column by which the query result should be indexed.
46
 * - [[asArray()]]: whether to return each record as an array.
47
 *
48
 * These options can be configured using methods of the same name. For example:
49
 *
50
 * ```php
51
 * $customers = Customer::find()->with('orders')->asArray()->all();
52
 * ```
53
 *
54
 * Relational query
55
 * ----------------
56
 *
57
 * In relational context ActiveQuery represents a relation between two Active Record classes.
58
 *
59
 * Relational ActiveQuery instances are usually created by calling [[ActiveRecord::hasOne()]] and
60
 * [[ActiveRecord::hasMany()]]. An Active Record class declares a relation by defining
61
 * a getter method which calls one of the above methods and returns the created ActiveQuery object.
62
 *
63
 * A relation is specified by [[link]] which represents the association between columns
64
 * of different tables; and the multiplicity of the relation is indicated by [[multiple]].
65
 *
66
 * If a relation involves a junction table, it may be specified by [[via()]] or [[viaTable()]] method.
67
 * These methods may only be called in a relational context. Same is true for [[inverseOf()]], which
68
 * marks a relation as inverse of another relation and [[onCondition()]] which adds a condition that
69
 * is to be added to relational query join condition.
70
 *
71
 * @author Christopher Mota <[email protected]>
72
 * @since 1.0.0
73
 */
74
class ActiveQuery extends Query implements ActiveQueryInterface
75
{
76
77
    use ActiveQueryTrait;
78
    use ActiveRelationTrait;
0 ignored issues
show
The trait yii\db\ActiveRelationTrait requires some properties which are not provided by chrmorandi\ldap\ActiveQuery: $join, $joinWith, $from
Loading history...
79
    /**
80
     * @event Event an event that is triggered when the query is initialized via [[init()]].
81
     */
82
    const EVENT_INIT = 'init';
83
84
    /**
85
     * Constructor.
86
     * @param string $modelClass the model class associated with this query
87
     * @param array $config configurations to be applied to the newly created query object
88
     */
89
    public function __construct($modelClass, $config = [])
90
    {
91
        $this->modelClass = $modelClass;
92
        parent::__construct($config);
93
    }
94
95
    /**
96
     * Initializes the object.
97
     * This method is called at the end of the constructor. The default implementation will trigger
98
     * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end
99
     * to ensure triggering of the event.
100
     */
101
    public function init()
102
    {
103
        parent::init();
104
        $this->trigger(self::EVENT_INIT);
105
    }
106
107
    /**
108
     * Executes query and returns all results as an array.
109
     * @param Connection $db the DB connection used to create the DB command.
110
     * If null, the DB connection returned by [[modelClass]] will be used.
111
     * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
112
     */
113
    public function all($db = null)
114
    {
115
        return parent::all($db);
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function populate($rows)
122
    {
123
        if (empty($rows)) {
124
            return [];
125
        }
126
127
        $models = $this->createModels($rows);
128
129
        if (!empty($this->with)) {
130
            $this->findWith($this->with, $models);
131
        }
132
        if (!$this->asArray) {
133
            foreach ($models as $model) {
134
                $model->afterFind();
135
            }
136
        }
137
138
        return $models;
139
    }
140
141
    /**
142
     * Executes the query and returns a single row of result.
143
     * @param Connection $db the database connection used to generate the SQL statement.
144
     * If this parameter is not given, the `db` application component will be used.
145
     * @return array|bool the first row (in terms of an array) of the query result. False is returned if the query
146
     * results in nothing.
147
     */
148
    public function one($db = null)
149
    {
150
        $row = parent::one($db);
151
        if ($row !== false) {
0 ignored issues
show
The condition $row !== false is always true.
Loading history...
152
            $models = $this->populate($row);
153
            return reset($models) ?: null;
154
        } else {
155
            return null;
156
        }
157
    }
158
159
}
160