-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.php
111 lines (96 loc) · 1.97 KB
/
model.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
<?php namespace Mongor;
class Model {
/**
* Database instance | config
*
* @var \Mongor\MangoDB|null|string
*/
protected $_db = 'default';
/**
* Active collection
*
* @var string
*/
protected $_collection = '';
public function __construct($db = NULL)
{
if ($db !== NULL)
{
$this->_db = $db;
}
if (is_string($this->_db))
{
$this->_db = MongoDB::instance($this->_db);
}
}
/**
* Insert a document
*
* @param array $insert
* @param bool $options
* @return MongoDB Object
*/
public function insert(array $insert, $options = true)
{
return $this->_db->insert($this->_collection, $insert, $options);
}
/**
* Get a single document
*
* @param array $get
* @param array $fields
* @return Mongodb Object
*/
public function get(array $get, array $fields = array())
{
return $this->_db->find_one($this->_collection, $get, $fields);
}
/**
* Update a document
*
* @param array $criteria
* @param array $update
* @param array $array
* @return null
*/
public function update(array $criteria, array $update, array $options = array())
{
return $this->_db->update($this->_collection, $criteria, $update, $options);
}
/**
* Find documents
*
* @param array $query
* @param array $fields
* @return MongoDB Object
*/
public function find($query = array(), $fields = array())
{
$find = $this->_db->find($this->_collection, $query, $fields);
if($find->count()==0)
{
return array();
}
return $find;
}
/**
* Delete a document
*
* @param array $criteria
* @return null
*/
public function delete(array$criteria)
{
return $this->_db->remove($this->_collection, $criteria, false);
}
/**
* Set an index for collection
*
* @param $keys
* @return void
*/
public function set_index($keys)
{
return $this->_db->ensure_index($this->_collection, $keys);
}
}