-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoModel.php
68 lines (49 loc) · 1.95 KB
/
MongoModel.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
<?php
class MongoModel extends MongoClient {
protected $collection = null;
protected $client = null;
public function __construct($collectionName){
//Can use your own config here or new properties for the class.
$config = Config::getInstance();
//$config::mongo = "mongodb://localhost:27017"
//$config::mongodb = "db";
$this->client = new MongoClient($config::$mongo, array("db"=>$config::$mongodb));
$this->collection = $this->client->selectDB($config::$mongodb)->selectCollection($collectionName);
}
public function update($critera, $new_object, $options = array()){
return $this->collection->update($critera,$new_object,$options);
}
public function insert($array,$options = array()){
$array['local_created_at'] = new MongoDate();
return $this->collection->insert($array,$options);
}
public function delete($array, $options = array()){
return $this->collection->remove($array, $options);
}
public function count($array){
return $this->collection->count($array);
}
public function explain($array,$options){
return $this->explain($array,$options);
}
public function find($query = array(),$fields = array(), $limit = false){
$data = $this->collection->find($query,$fields);
if ($limit != false) {
return $this->toArray(iterator_to_array($data->limit($limit)));
}
return $this->toArray(iterator_to_array($data));
}
private function toArray($data) {
$array = array();
foreach ($data as $item) {
$array[] = $item;
}
return $array;
}
public function findAll($query = array(),$fields = array()){
return iterator_to_array($this->collection->find($query,$fields));
}
public function findOne($query = array(), $fields = array()) {
return iterator_to_array($this->collection->findOne($query,$fields));
}
}