1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ActiveRecord for API |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\InvalidConfigException; |
15
|
|
|
use yii\db\QueryInterface; |
16
|
|
|
|
17
|
|
|
class ActiveDataProvider extends \yii\data\ActiveDataProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ActiveQuery the query that is used to fetch data models and [[totalCount]] |
21
|
|
|
* if it is not explicitly set |
22
|
|
|
*/ |
23
|
|
|
public $query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* To improve performance, implemented grid summary and pager loading via AJAX when this attribute is `false` |
27
|
|
|
* There is a possibility set this attribute via DI |
28
|
|
|
* @see \hipanel\base\SearchModelTrait::search() |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
public bool $countSynchronously = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool|mixed |
36
|
|
|
*/ |
37
|
|
|
private bool $loadModels = true; |
38
|
|
|
|
39
|
|
|
public function enableSynchronousCount(): void |
40
|
|
|
{ |
41
|
|
|
$this->countSynchronously = true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function preventLoadModels(): void |
45
|
|
|
{ |
46
|
|
|
$this->loadModels = false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* When receiving the pager and summary through AJAX, to calculate the limit and offset of Grid, |
51
|
|
|
* you need to get the maximum possible total count, otherwise the Grid pages will not switch |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
* @throws \yii\base\InvalidConfigException |
55
|
|
|
*/ |
56
|
|
|
protected function prepareTotalCount(): int |
57
|
|
|
{ |
58
|
|
|
if ($this->countSynchronously) { |
59
|
|
|
$app = Yii::$app; |
60
|
|
|
|
61
|
|
|
return $app->getCache()->getOrSet([$app->user->getId(), $this->query->modelClass, __METHOD__], fn(): int => parent::prepareTotalCount(), 5); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return PHP_INT_MAX; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** {@inheritdoc} */ |
68
|
|
|
protected function prepareModels(): array |
69
|
|
|
{ |
70
|
|
|
if (!$this->query instanceof QueryInterface) { |
71
|
|
|
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.'); |
72
|
|
|
} |
73
|
|
|
$query = clone $this->query; |
74
|
|
|
if (($pagination = $this->getPagination()) !== false) { |
75
|
|
|
$pagination->totalCount = $this->getTotalCount(); |
76
|
|
|
if ($pagination->totalCount === 0) { |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
$query->limit($pagination->getLimit())->offset($pagination->getOffset()); |
80
|
|
|
} |
81
|
|
|
if (($sort = $this->getSort()) !== false) { |
82
|
|
|
$query->addOrderBy($sort->getOrders()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->loadModels ? $query->all($this->db) : array_pad([], $pagination->pageSize, 0); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|