-
Notifications
You must be signed in to change notification settings - Fork 7
/
Mongo_ModelBase.php
312 lines (285 loc) · 9.68 KB
/
Mongo_ModelBase.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
class Mongo_ModelBase {
public static $_mongo = null;
public static $_collection = null;
public static $_collectionName = null;
const DB_SERVER = 'localhost';
const DB_NAME = 'db';
const DB_PORT = '27017';
protected $id = null;
protected $document = null;
/*************************************************************************************
* Magic methods
*************************************************************************************/
/**
* Constructor puts full object in $document variable and assigns $id
* @param $document
*/
public function __construct($document = null) {
if($document != null && isset($document['_id'])){
$this->document = $document;
$this->id = $this->document['_id'];
unset($this->document['_id']);
}
elseif($document !== null){
$this->document = $document;
}
else {
$this->document = array();
}
}
/**
* Return object ID
*/
public function __toString(){
return ucfirst(static::$_collectionName) . "Object ID:" . $this->id;
}
/**
* Get values like an object
* @param string $name
*/
public function __get($name){
if($name == "id" || $name == "_id"){
return $this->id->__toString();
}
if (false !== strpos($name, '.')) {
return $this->_getDotNotation($name, $this->document);
}
return isset($this->document[$name])?$this->document[$name]:null;
}
/**
* Set values like an object
* @param string $name
* @param mixed $val
*/
public function __set($name, $val){
if(false !== strpos($name, '.')){
return $this->_setDotNotation($name, $val, $this->document);
}
if($val == null){
unset($this->document[$name]);
}
$this->document[$name] = $val;
}
/**
* Check if variable is set in object
* @param string $name
*/
public function __isset($name){
return isset($this->document[$name]);
}
/**
* Unset a variable in the object
* @param $name
*/
public function __unset($name){
unset($this->document[$name]);
}
/**
* Allows use of the dot notation in the __get function
* Thanks to Ian White for this function: https://github.com/ibwhite/simplemongophp
* @param string $fields fields with dot notation
* @param reference $current The current part of the array working in
*/
protected function _getDotNotation($fields, &$current){
$i = strpos($fields, '.');
if ($i !== false) {
$field = substr($fields, 0, $i);
if (!isset($current[$field])) {
return null;
}
$current =& $current[$field];
return $this->_getDotNotation(substr($fields, $i+1), $current);
} else {
return isset($current[$fields]) ? $current[$fields] : null;
}
}
/**
* Allows use of the not notation in __set function
* Thanks to Ian White for this function: https://github.com/ibwhite/simplemongophp
* @param string $fields
* @param mixed $value
* @param reference $current
*/
protected function _setDotNotation($fields, $value, &$current){
$i = strpos($fields, '.');
if ($i !== false) {
$field = substr($fields, 0, $i);
if (!isset($current[$field])) {
$current[$field] = array();
}
$current =& $current[$field];
return $this->_setDotNotation(substr($fields, $i+1), $value, $current);
} else {
$current[$fields] = $value;
}
}
/***********************************************************************************
* Object Methods
************************************************************************************/
/**
* Delete the object
*/
public function delete(){
if($this->id != null){
static::$_collection->remove(array("_id"=>$this->id));
}
}
/**
* Save the object with all variables that have been set
*/
public function save(){
if($this->id == null){
static::insert($this->document, true);
$this->id = $this->document['_id'];
unset($this->document['_id']);
return true;
}
else {
return static::update(array("_id"=>$this->id), $this->document);
}
}
/**
* Do special updates to the object (incrementing, etc...)
* @param mixed $data
*/
public function specialUpdate($modifier, $options){
return static::update(array("_id"=>$this->id), $modifier, $options);
}
/*************************************************************************************
* Static methods
*************************************************************************************/
/**
* Connect to mongo...
*/
protected static function connect(){
$mongoDns = sprintf('mongodb://%s:%s',
self::DB_SERVER, self::DB_PORT
);
$connection = new Mongo($mongoDns,array("persist" => "x"));
self::$_mongo = $connection->selectDB(self::DB_NAME);
//If collection name isn't already set in the model
if(static::$_collectionName == null){
//Get collection name based on the class name. To do this, we take the class name and strip off the
//beginning "Model_" and the rest is the collection name.
$replaceableClassNameparts = array('model_');
static::$_collectionName = str_replace($replaceableClassNameparts, '', strtolower(get_called_class()));
}
$collectionName = static::$_collectionName;
static::$_collection = self::$_mongo->$collectionName;
}
/**
* Setup db connection and init mongo collection
*/
public static function init(){
if(self::$_mongo == null){
self::connect();
}
}
/**
* Load object by ID
* @param $_id
*/
public static function load($_id){
$object = static::findOne(array("_id"=>new MongoId($_id)));
if($object === null){
return false;
}
else{
return $object;
}
}
/**
* Find all records in a collection
*/
public static function findAll(){
return static::find();
}
/**
* Get one record
*/
public static function findOne($conditionalArray = null, $fieldsArray = null, $sort = null){
$className = get_called_class();
$document = static::getCursor($conditionalArray, $fieldsArray, true);
if($document == null){
return null;
}
$object = new $className($document);
return $object;
}
/**
* Query the database for documents in the collection
* @param array $conditionalArray
* @param array $fieldsArray
* @param array $sort
* @param int $limit
*/
public static function find($conditionalArray = null, $fieldsArray = null, $sort = null, $limit = null){
$cursor = static::getCursor($conditionalArray, $fieldsArray);
if($limit != null){
$cursor = $cursor->limit($limit);
}
if($sort != null){
$cursor = $cursor->sort($sort);
}
$className = get_called_class();
$objectArray = array();
foreach($cursor as $document){
$objectArray[] = new $className($document);
}
return $objectArray;
}
/**
* Count by query array
* @param array $conditionalArray
*/
public static function count($conditionalArray = null){
$cursor = static::getCursor($conditionalArray);
return $cursor->count();
}
/**
* Create cursor by query document
* @param array $conditionalArray
* @param array $fieldsArray
*/
protected static function getCursor($conditionalArray = null, $fieldsArray = null, $one=false){
static::init();
if($conditionalArray == null) $conditionalArray = array();
if($fieldsArray == null) $fieldsArray = array();
if($one){
return static::$_collection->findOne($conditionalArray, $fieldsArray);
}
$cursor = static::$_collection->find($conditionalArray, $fieldsArray);
return $cursor;
}
/**
*
* Enter description here ...
* @param array $data
* @param bool $safe // Set true if you want to wait for database response...
* @param bool $fsync
*/
public static function insert($data, $safe = false, $fsync = false){
static::init();
$options = array();
if($safe){
$options['safe'] = true;
}
if($fsync){
$options['fsync'] = true;
}
return static::$_collection->insert($data, $options);
}
/**
* Do a batch insert into the collection
* @param array $data
*/
public static function batchInsert($data){
static::init();
return static::$_collection->batchInsert($data);
}
public static function update($criteria, $update, $options = array()){
static::init();
return static::$_collection->update($criteria, $update, $options);
}
}