Completed
Push — master ( 808509...f69da3 )
by Christopher
06:05
created

ActiveDataProvider::prepareTotalCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * @link      https://github.com/chrmorandi/yii2-ldap for the canonical 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
 */
8
9
namespace chrmorandi\ldap;
10
11
use yii\base\InvalidConfigException;
12
use yii\db\QueryInterface;
13
14
/**
15
 * ActiveDataProvider implements a data provider based on [[\yii\db\Query]] and [[\yii\db\ActiveQuery]].
16
 *
17
 * ActiveDataProvider provides data by performing DB queries using [[query]].
18
 *
19
 * The following is an example of using ActiveDataProvider to provide ActiveRecord instances:
20
 *
21
 * ```php
22
 * $provider = new ActiveDataProvider([
23
 *     'query' => Post::find(),
24
 *     'pagination' => [
25
 *         'pageSize' => 20,
26
 *     ],
27
 * ]);
28
 *
29
 * // get the posts in the current page
30
 * $posts = $provider->getModels();
31
 * ```
32
 *
33
 * And the following example shows how to use ActiveDataProvider without ActiveRecord:
34
 *
35
 * ```php
36
 * $query = new Query();
37
 * $provider = new ActiveDataProvider([
38
 *     'query' => $query->from('post'),
39
 *     'pagination' => [
40
 *         'pageSize' => 20,
41
 *     ],
42
 * ]);
43
 *
44
 * // get the posts in the current page
45
 * $posts = $provider->getModels();
46
 * ```
47
 *
48
 * @author Christopher Mota <[email protected]>
49
 * @since 1.0.0
50
 */
51
class ActiveDataProvider extends \yii\data\ActiveDataProvider
52
{
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function prepareTotalCount()
57
    {
58
        if (!$this->query instanceof QueryInterface) {
59
            throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
60
        }
61
        return (int) $this->query->limit($this->getPagination()->getLimit())->offset(-1)->orderBy([])->count('*', $this->db);
62
    }
63
64
}
65