-
Notifications
You must be signed in to change notification settings - Fork 12
/
DataProvider.php
195 lines (173 loc) · 4.48 KB
/
DataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace sammaye\mongoyii;
use Yii;
use CActiveDataProvider;
use sammaye\mongoyii\Document;
use sammaye\mongoyii\Query;
/**
* EMongoDataProvider
*
* A data Provider helper for interacting with the EMongoCursor
*/
class DataProvider extends CActiveDataProvider
{
/**
* The primary ActiveRecord class name. The {@link getData()} method
* will return a list of objects of this class.
* @var string
*/
public $modelClass;
/**
* The AR finder instance (eg <code>Post::model()</code>).
* This property can be set by passing the finder instance as the first parameter
* to the constructor. For example, <code>Post::model()->published()</code>.
* @var Model
*/
public $model;
/**
* The name of key attribute for {@link modelClass}. If not set,
* it means the primary key of the corresponding database table will be used.
* @var string
*/
public $keyAttribute = '_id';
/**
* @var array The criteria array
*/
private $_criteria;
/**
* The internal MongoDB cursor as a MongoCursor instance
* @var \MongoCursor
*/
private $_cursor;
/**
* @var Sort
*/
private $_sort;
/**
* Creates the EMongoDataProvider instance
* @param string|Document $modelClass
* @param array $config
*/
public function __construct($modelClass, $config = array())
{
if(is_string($modelClass)){
$this->modelClass = $modelClass;
$this->model = Document::model($this->modelClass);
}elseif($modelClass instanceof Document){
$this->modelClass = get_class($modelClass);
$this->model = $modelClass;
}
$this->setId($this->modelClass);
foreach($config as $key => $value){
$this->$key = $value;
}
if(!$this->getCriteria()){
$this->setCriteria(new Query);
}
}
/**
* @see CActiveDataProvider::getCriteria()
* @return array|Query
*/
public function getCriteria()
{
return $this->_criteria;
}
/**
* @see CActiveDataProvider::setCriteria()
* @param array|Query $value
* @throws Exception
*/
public function setCriteria($value)
{
if(is_array($value)){
$this->_criteria = new Query($value);
}elseif($value instanceof Query){
$this->_criteria = $value;
}else{
throw new Exception(
'Criteria for the mongoyii\DataProvider must be either an array or a mongoyii\Query object'
);
}
}
/**
* @see CActiveDataProvider::fetchData()
* @return array
*/
public function fetchData()
{
$criteria = $this->getCriteria();
if(($pagination = $this->getPagination()) !== false){
$pagination->setItemCount($this->getTotalItemCount());
$criteria->limit = $pagination->getLimit();
$criteria->skip = $pagination->getOffset();
}
if(($sort = $this->getSort()) !== false){
$sort = $sort->getOrderBy();
if(count($sort) > 0){
$criteria->sort = $sort;
}
}
$c = $this->model->find($criteria);
return iterator_to_array(
$c,
false
);
}
/**
* @see CActiveDataProvider::fetchKeys()
* @return array
*/
public function fetchKeys()
{
$keys = array();
foreach($this->getData() as $i => $data){
$key = $this->keyAttribute === null ? $data->{$data->primaryKey()} : $data->{$this->keyAttribute};
$keys[$i] = is_array($key) ? implode(',', $key) : $key;
}
return $keys;
}
/**
* @see CActiveDataProvider::calculateTotalItemCount()
* @return int
*/
public function calculateTotalItemCount()
{
$criteria = $this->getCriteria();
return $this->model->count($criteria->getCondition());
}
public function setSort($value)
{
if(is_array($value))
{
if(isset($value['class']))
{
$sort=$this->getSort($value['class']);
unset($value['class']);
}
else
$sort=$this->getSort();
foreach($value as $k=>$v)
$sort->$k=$v;
}
else
$this->_sort=$value;
}
/**
* Returns the sort object. We don't use the newer getSort function because it does not have the same functionality
* between 1.1.10 and 1.1.13, the functionality we need is actually in 1.1.13 only
* @param string $className
* @return \CSort|Sort|false - the sorting object. If this is false, it means the sorting is disabled.
*/
public function getSort($className = 'sammaye\mongoyii\Sort')
{
if($this->_sort === null){
$this->_sort = new $className;
if(($id = $this->getId()) != ''){
$this->_sort->sortVar = $id . '_sort';
}
$this->_sort->modelClass = $this->modelClass;
}
return $this->_sort;
}
}