This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
MongoRecord.php
563 lines (507 loc) · 17.2 KB
/
MongoRecord.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
/**
* Description of MongoRecord
*
* @author yohan
*/
abstract class MongoRecord extends CModel
{
public static $db;
public $safe=TRUE;
public $fsync=TRUE;
public $limit=0;
public $skip=0;
public $useCursor=TRUE;
private $_sort;
private $_new;
/**
* Set this property to FALSE if you have defined _id variable as object ID
*/
public $bsonID=TRUE;
/**
* $_document property to store MongoDB document. Must defined as array in each MongoRecord model.
*/
protected $_document;
private static $_models=array();
public function getDb()
{
return Yii::app()->getComponent('mongodb')->db;
}
/**
* Convert collection record from array multi level to flat array.
* Record will only flatted when the array key is not numeric key
* For flatted value it's seperated with "." to identified it's parent
*/
protected function parseAttributes($attributes,$prefix='')
{
$flatAttributes=array();
foreach($attributes as $key=>$value)
{
if(is_numeric($key) && is_array($value))
$flatAttributes=array_merge($flatAttributes, $this->parseAttributes($value,$key.'.'));
else
$flatAttributes[$key]=$value;
}
return $flatAttributes;
}
public function __construct($arg='insert')
{
if($arg===null) // internally used by populateRecord() and model()
return;
elseif(is_array($arg))
{
$this->_document=$arg;
return;
}
$this->setScenario($arg);
$this->setIsNewRecord(true);
//$this->init();
$this->attachBehaviors($this->behaviors());
$this->afterConstruct();
}
/**
* PHP getter magic method.
* This method is overridden so that MongoDB document can be accessed like properties.
* @param string property name
* @return mixed property value
* @see getAttribute
*/
public function __get($name)
{
if($name==='id')
return $this->_document['_id'];
elseif(array_key_exists($name,$this->_document))
return $this->_document[$name];
else
return parent::__get($name);
}
/**
* PHP setter magic method.
* This method is overridden so that MongoDB document can be accessed like properties.
* @param string property name
* @param mixed property value
*/
public function __set($name,$value)
{
if($name==='id')
$this->_document['_id']=$value;
elseif(array_key_exists($name,$this->_document))
$this->_document[$name]=$value;
else
parent::__set($name,$value);
}
public function getAttributes($names=true)
{
$doc=$this->_document;
if(is_array($names))
{
$attrs=array();
foreach($names as $name)
{
if(property_exists($this,$name))
$attrs[$name]=$this->$name;
else
$attrs[$name]=isset($doc[$name])?$doc[$name]:null;
}
return $attrs;
}
else
return $doc;
}
abstract protected function getCollectionName();
public function getCollection()
{
return $this->db->{$this->collectionName};
}
public function getIsNewRecord()
{
return $this->_new;
}
public function save($runValidation=true,$attributes=null)
{
if(!$runValidation || $this->validate($attributes))
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
else
return false;
}
/**
* Save document in $this->_document to database
*/
public function insert($attributes=null)
{
if($this->beforeSave())
{
try
{
$insert=$this->collection->insert($this->getAttributes($attributes),array('fsync'=>$this->fsync,'safe'=>$this->safe));
$this->_id=$this->_document['_id'];
$this->afterSave();
return TRUE;
}
catch (MongoCursorException $e)
{
throw new CDbException($e->getMessage(), $e->getCode());
return FALSE;
}
}
return FALSE;
}
/**
* Update record in database with data from document
* @return boolean whether record is succesfully updated or not
*/
public function update($attributes=null)
{
if($this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));
if($this->beforeSave())
{
$doc=$this->_document;
$id=($this->bsonID)?new MongoId($this->id):$this->id;
try
{
$update=$this->collection->update(array('_id'=>$id),array('$set'=>$doc),array('fsync'=>$this->fsync,'multiple'=>FALSE));
$this->afterSave();
return TRUE;
}
catch (MongoCursorException $e)
{
throw new CDbException($e->getMessage(), $e->getCode());
return FALSE;
}
}
return FALSE;
}
/**
* Update record in database with data from document
* @return boolean whether record is succesfully updated or not
*/
public function updateAll($criteria,$document)
{
return $this->collection->update($criteria,array('$set' => $document),array('safe'=>$this->safe,'fsync'=>$this->fsync,'multiple'=>TRUE));
}
/**
* Remove record from database
* @return boolean whether record is succesfully removed or not
*/
public function delete()
{
if(!$this->getIsNewRecord())
{
Yii::trace(get_class($this).'.delete()','system.db.ar.CActiveRecord');
if($this->beforeDelete())
{
$result=$this->deleteById($this->id);
$this->afterDelete();
return $result;
}
else
return false;
}
else
throw new CDbException(Yii::t('yii','The active record cannot be deleted because it is new.'));
}
/**
* Remove all record that match with criteria from database
* @param array record criteria to remove
* @return int return number of deleted record
*/
public function deleteAll($criteria)
{
$result=$this->collection->remove($criteria,array('justOne'=>FALSE,'safe'=>$this->safe));
if(is_array($result) && isset($result['n']))
return $result['n'];
else
return TRUE;
}
/**
* Remove record with specific $id from database
* @param string record id to remove
* @return boolean whether record is succesfully removed or not
*/
public function deleteById($id)
{
if($this->bsonID)
$id=new MongoId($id);
$result= $this->collection->remove(array('_id'=>$id),array('justOne'=>TRUE,'safe'=>$this->safe));
if(is_array($result) && isset($result['ok']))
return $result['ok'];
else
return TRUE;
}
/**
* Refresh record by repull data from database
*/
public function refresh()
{
Yii::trace(get_class($this).'.refresh()','system.db.ar.CActiveRecord');
if(!$this->getIsNewRecord() && ($record=$this->findByid($this->_id))!==null)
{
return true;
}
else
return false;
}
/**
* Merge attributes with document
*/
public function saveAttributes($attributes=array())
{
$this->_document=array_merge($this->_document,$attributes);
}
/**
* @param boolean whether the record is new and should be inserted when calling {@link save}.
* @see getIsNewRecord
*/
public function setIsNewRecord($value)
{
$this->_new=$value;
}
/**
* This event is raised before the record is saved.
* @param CEvent the event parameter
* @since 1.0.2
*/
public function onBeforeSave($event)
{
$this->raiseEvent('onBeforeSave',$event);
}
/**
* This event is raised after the record is saved.
* @param CEvent the event parameter
* @since 1.0.2
*/
public function onAfterSave($event)
{
$this->raiseEvent('onAfterSave',$event);
}
/**
* This event is raised before the record is deleted.
* @param CEvent the event parameter
* @since 1.0.2
*/
public function onBeforeDelete($event)
{
$this->raiseEvent('onBeforeDelete',$event);
}
/**
* This event is raised after the record is deleted.
* @param CEvent the event parameter
* @since 1.0.2
*/
public function onAfterDelete($event)
{
$this->raiseEvent('onAfterDelete',$event);
}
/**
* This event is raised after the record instance is created by new operator.
* @param CEvent the event parameter
* @since 1.0.2
*/
public function onAfterConstruct($event)
{
$this->raiseEvent('onAfterConstruct',$event);
}
/**
* This event is raised before an AR finder performs a find call.
* @param CEvent the event parameter
* @see beforeFind
* @since 1.0.9
*/
public function onBeforeFind($event)
{
$this->raiseEvent('onBeforeFind',$event);
}
/**
* This event is raised after the record is instantiated by a find method.
* @param CEvent the event parameter
* @since 1.0.2
*/
public function onAfterFind($event)
{
$this->setIsNewRecord(FALSE);
$this->raiseEvent('onAfterFind',$event);
}
/**
* This method is invoked before saving a record (after validation, if any).
* The default implementation raises the {@link onBeforeSave} event.
* You may override this method to do any preparation work for record saving.
* Use {@link isNewRecord} to determine whether the saving is
* for inserting or updating record.
* Make sure you call the parent implementation so that the event is raised properly.
* @return boolean whether the saving should be executed. Defaults to true.
*/
protected function beforeSave()
{
if($this->hasEventHandler('onBeforeSave'))
{
$event=new CModelEvent($this);
$this->onBeforeSave($event);
return $event->isValid;
}
else
return true;
}
/**
* This method is invoked after saving a record successfully.
* The default implementation raises the {@link onAfterSave} event.
* You may override this method to do postprocessing after record saving.
* Make sure you call the parent implementation so that the event is raised properly.
*/
protected function afterSave()
{
if($this->hasEventHandler('onAfterSave'))
$this->onAfterSave(new CEvent($this));
}
/**
* This method is invoked before deleting a record.
* The default implementation raises the {@link onBeforeDelete} event.
* You may override this method to do any preparation work for record deletion.
* Make sure you call the parent implementation so that the event is raised properly.
* @return boolean whether the record should be deleted. Defaults to true.
*/
protected function beforeDelete()
{
if($this->hasEventHandler('onBeforeDelete'))
{
$event=new CModelEvent($this);
$this->onBeforeDelete($event);
return $event->isValid;
}
else
return true;
}
/**
* This method is invoked after deleting a record.
* The default implementation raises the {@link onAfterDelete} event.
* You may override this method to do postprocessing after the record is deleted.
* Make sure you call the parent implementation so that the event is raised properly.
*/
protected function afterDelete()
{
if($this->hasEventHandler('onAfterDelete'))
$this->onAfterDelete(new CEvent($this));
}
/**
* This method is invoked after a record instance is created by new operator.
* The default implementation raises the {@link onAfterConstruct} event.
* You may override this method to do postprocessing after record creation.
* Make sure you call the parent implementation so that the event is raised properly.
*/
protected function afterConstruct()
{
if($this->hasEventHandler('onAfterConstruct'))
$this->onAfterConstruct(new CEvent($this));
}
/**
* Creates an active record instance.
* This method is called by {@link populateRecord} and {@link populateRecords}.
* You may override this method if the instance being created
* depends the attributes that are to be populated to the record.
* For example, by creating a record based on the value of a column,
* you may implement the so-called single-table inheritance mapping.
* @param array list of attribute values for the active records.
* @return CActiveRecord the active record
* @since 1.0.2
*/
protected function instantiate($document)
{
$class=get_class($this);
$model=new $class(null);
$model->_document=array_merge($this->_document,$document);
$model->afterFind();
return $model;
}
/**
* This method is invoked before an AR finder executes a find call.
* The find calls include {@link find}, {@link findAll}, {@link findByPk},
* {@link findAllByPk}, {@link findByAttributes} and {@link findAllByAttributes}.
* The default implementation raises the {@link onBeforeFind} event.
* If you override this method, make sure you call the parent implementation
* so that the event is raised properly.
* @since 1.0.9
*/
protected function beforeFind()
{
if($this->hasEventHandler('onBeforeFind'))
$this->onBeforeFind(new CEvent($this));
}
/**
* This method is invoked after each record is instantiated by a find method.
* The default implementation raises the {@link onAfterFind} event.
* You may override this method to do postprocessing after each newly found record is instantiated.
* Make sure you call the parent implementation so that the event is raised properly.
*/
protected function afterFind()
{
if($this->hasEventHandler('onAfterFind'))
$this->onAfterFind(new CEvent($this));
}
public function find($query=array())
{
$doc=$this->collection->findOne($query);
if($doc!==NULL)
return $this->instantiate($doc);
else
return NULL;
}
public function count($query=array())
{
return $this->collection->count($query, $this->limit,$this->skip);
}
public function findById($id)
{
return $this->find(array('_id'=>new MongoId($id)));
}
public function limit($limit)
{
$this->limit=$limit;
return $this;
}
public function skip($skip)
{
$this->skip=$skip;
return $this;
}
public function findAll($criteria=array())
{
if(isset($criteria['query']))
$docs=$this->collection->find($criteria['query']);
else
$docs=$this->collection->find($criteria);
if(isset($criteria['sort']))
$docs=$docs->sort($criteria['sort']);
else if(!empty($this->_sort))
$docs=$docs->sort($this->_sort);
if(isset($criteria['limit']) || $this->limit>0)
$docs=$docs->limit(isset($criteria['limit'])?$criteria['limit']:$this->limit);
if(isset($criteria['skip']) || $this->skip>0)
$docs=$docs->skip(isset($criteria['skip'])?$criteria['skip']:$this->skip);
return $this->populateRecords($docs);
}
protected function populateRecords($documents)
{
$records=array();
foreach($documents as $doc)
{
$records[]=$this->instantiate($doc);
}
return $records;
}
public static function model($className=__CLASS__)
{
if(isset(self::$_models[$className]))
return self::$_models[$className];
else
{
$model=self::$_models[$className]=new $className(null);
$model->attachBehaviors($model->behaviors());
return $model;
}
}
public function sort($field=array())
{
$this->_sort=$field;
return $this;
}
}