1
|
|
|
<?php |
2
|
|
|
namespace Data; |
3
|
|
|
|
4
|
|
|
class MongoDataTable extends DataTable |
5
|
|
|
{ |
6
|
|
|
protected $collection; |
7
|
|
|
protected $name; |
8
|
|
|
|
9
|
|
|
public function __construct($collection, $collection_name = false) |
10
|
|
|
{ |
11
|
|
|
$this->collection = $collection; |
12
|
|
|
$this->name = $collection_name; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function count($filter = false) |
16
|
|
|
{ |
17
|
|
|
$criteria = array(); |
18
|
|
|
if($filter !== false) |
19
|
|
|
{ |
20
|
|
|
if($filter instanceof \Data\Filter) |
21
|
|
|
{ |
22
|
|
|
$criteria = $filter->to_mongo_filter(); |
23
|
|
|
} |
24
|
|
|
else |
25
|
|
|
{ |
26
|
|
|
$criteria = $filter; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return $this->collection->count($criteria, array(), $this->name); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function getCriteriaFromFilter($filter) |
33
|
|
|
{ |
34
|
|
|
if($filter === false) |
35
|
|
|
{ |
36
|
|
|
return array(); |
37
|
|
|
} |
38
|
|
|
if(is_array($filter)) |
39
|
|
|
{ |
40
|
|
|
return $filter; |
41
|
|
|
} |
42
|
|
|
return $filter->to_mongo_filter(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function read($filter = false, $select = false, $count = false, $skip = false, $sort = false, $params = false) |
46
|
|
|
{ |
47
|
|
|
$fields = array(); |
48
|
|
|
$criteria = $this->getCriteriaFromFilter($filter); |
49
|
|
|
if($select !== false) |
50
|
|
|
{ |
51
|
|
|
$fields = array_fill_keys($select, 1); |
52
|
|
|
} |
53
|
|
|
$cursor = $this->collection->find($criteria, $fields, $this->name); |
54
|
|
|
if($params !== false && isset($params['fields'])) |
55
|
|
|
{ |
56
|
|
|
$cursor->fields($params['fields']); |
57
|
|
|
} |
58
|
|
|
if($sort !== false) |
59
|
|
|
{ |
60
|
|
|
$cursor->sort($sort); |
61
|
|
|
} |
62
|
|
|
if($skip !== false) |
63
|
|
|
{ |
64
|
|
|
$cursor->skip($skip); |
65
|
|
|
} |
66
|
|
|
if($count !== false) |
67
|
|
|
{ |
68
|
|
|
$cursor->limit($count); |
69
|
|
|
} |
70
|
|
|
if(method_exists($cursor, 'setTypeMap')) |
71
|
|
|
{ |
72
|
|
|
$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']); |
73
|
|
|
return $cursor->toArray(); |
74
|
|
|
} |
75
|
|
|
$ret = array(); |
76
|
|
|
foreach($cursor as $doc) |
77
|
|
|
{ |
78
|
|
|
array_push($ret, $doc); |
79
|
|
|
} |
80
|
|
|
return $ret; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function create($data) |
84
|
|
|
{ |
85
|
|
|
$res = $this->collection->insert($data); |
86
|
|
|
if($res === false || $res['err'] !== null) |
87
|
|
|
{ |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
return $data['_id']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function update($filter, $data) |
94
|
|
|
{ |
95
|
|
|
$criteria = $this->getCriteriaFromFilter($filter); |
96
|
|
|
if(isset($data['_id'])) |
97
|
|
|
{ |
98
|
|
|
unset($data['_id']); |
99
|
|
|
} |
100
|
|
|
$res = $this->collection->update($criteria, array('$set' => $data)); |
101
|
|
|
if($res === false || $res['err'] !== null) |
102
|
|
|
{ |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function delete($filter) |
109
|
|
|
{ |
110
|
|
|
$criteria = $this->getCriteriaFromFilter($filter); |
111
|
|
|
$res = $this->collection->remove($criteria); |
112
|
|
|
if($res === false || $res['err'] !== null) |
113
|
|
|
{ |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|