-
Notifications
You must be signed in to change notification settings - Fork 75
/
EMongoDataProvider.php
198 lines (178 loc) · 5.22 KB
/
EMongoDataProvider.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
196
197
198
<?php
/**
* EMongoDataProvider
*
* A data Provider helper for interacting with the EMongoCursor
*/
class EMongoDataProvider 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 EMongoModel
*/
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 EMongoCursor|MongoCursor
*/
private $_cursor;
/**
* @var EMongoSort
*/
private $_sort;
/**
* Creates the EMongoDataProvider instance
* @param string|EMongoDocument $modelClass
* @param array $config
*/
public function __construct($modelClass, $config = array())
{
if(is_string($modelClass)){
$this->modelClass = $modelClass;
$this->model = EMongoDocument::model($this->modelClass);
}elseif($modelClass instanceof EMongoDocument){
$this->modelClass = get_class($modelClass);
$this->model = $modelClass;
}
$this->setId($this->modelClass);
foreach($config as $key => $value){
$this->$key = $value;
}
}
/**
* @see CActiveDataProvider::getCriteria()
* @return array
*/
public function getCriteria()
{
return $this->_criteria;
}
/**
* @see CActiveDataProvider::setCriteria()
* @param array|EMongoCriteria $value
*/
public function setCriteria($value)
{
if($value instanceof EMongoCriteria){
$this->_criteria = $value->toArray();
}
if(is_array($value)){
$this->_criteria = $value;
}
}
/**
* @see CActiveDataProvider::fetchData()
* @return array
*/
public function fetchData()
{
$criteria = $this->getCriteria();
// I have not refactored this line considering that the condition may have changed from total item count to here, maybe.
$this->_cursor = $this->model->find(
isset($criteria['condition']) && is_array($criteria['condition']) ? $criteria['condition'] : array(),
isset($criteria['project']) && !empty($criteria['project']) ? $criteria['project'] : array()
);
// If we have sort and limit and skip setup within the incoming criteria let's set it
if(isset($criteria['sort']) && is_array($criteria['sort'])){
$this->_cursor->sort($criteria['sort']);
}
if(isset($criteria['skip']) && is_int($criteria['skip'])){
$this->_cursor->skip($criteria['skip']);
}
if(isset($criteria['limit']) && is_int($criteria['limit'])){
$this->_cursor->limit($criteria['limit']);
}
if(isset($criteria['hint']) && (is_array($criteria['hint']) || is_string($criteria['hint']))){
$this->_cursor->hint($criteria['hint']);
}
if(($pagination = $this->getPagination()) !== false){
$pagination->setItemCount($this->getTotalItemCount());
$this->_cursor->limit($pagination->getLimit());
$this->_cursor->skip($pagination->getOffset());
}
if(($sort = $this->getSort()) !== false){
$sort = $sort->getOrderBy();
if(count($sort) > 0){
$this->_cursor->sort($sort);
}
}
return iterator_to_array($this->_cursor, 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()
{
if(!$this->_cursor){
$criteria = $this->getCriteria();
$this->_cursor = $this->model->find(isset($criteria['condition']) && is_array($criteria['condition']) ? $criteria['condition'] : array());
}
return $this->_cursor->count();
}
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|EMongoSort|false - the sorting object. If this is false, it means the sorting is disabled.
*/
public function getSort($className = 'EMongoSort')
{
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;
}
}