1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Database; |
4
|
|
|
|
5
|
|
|
use DrMVC\Config\ConfigInterface; |
6
|
|
|
use DrMVC\Database\Drivers\QueryInterface; |
7
|
|
|
|
8
|
|
|
class Model implements ModelInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var QueryInterface |
12
|
|
|
*/ |
13
|
|
|
private $_instance; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default connection name |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $connection = Database::DEFAULT_CONNECTION; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Name of collection for query |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $collection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Model constructor. |
29
|
|
|
* @param ConfigInterface $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ConfigInterface $config) |
32
|
|
|
{ |
33
|
|
|
// get current connection |
34
|
|
|
$connection = $this->getConnection(); |
35
|
|
|
// Get config of required connection |
36
|
|
|
$config_db = $config->get($connection); |
37
|
|
|
try { |
38
|
|
|
if (null === $config_db) { |
39
|
|
|
throw new Exception("Connection \"$connection\" is not found in database config"); |
40
|
|
|
} |
41
|
|
|
} catch (Exception $e) { |
42
|
|
|
// __constructor |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Create database object with config from above |
46
|
|
|
$database = new Database($config_db); |
47
|
|
|
|
48
|
|
|
// Get current collection name |
49
|
|
|
$collection = $this->getCollection(); |
50
|
|
|
// Extract instance created by driver and put collection name |
51
|
|
|
$instance = $database->getInstance($collection); |
52
|
|
|
|
53
|
|
|
// Keep driver's instance as local parameter |
54
|
|
|
$this->setInstance($instance); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get current database name |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getConnection(): string |
63
|
|
|
{ |
64
|
|
|
return $this->connection; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get current collection |
69
|
|
|
* |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
|
|
public function getCollection() |
73
|
|
|
{ |
74
|
|
|
$collection = $this->collection; |
75
|
|
|
try { |
76
|
|
|
if (null === $collection) { |
77
|
|
|
throw new Exception('Collection is not set'); |
78
|
|
|
} |
79
|
|
|
} catch (Exception $e) { |
80
|
|
|
// __constructor |
81
|
|
|
} |
82
|
|
|
return $collection; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set database instance |
87
|
|
|
* |
88
|
|
|
* @param QueryInterface $instance |
89
|
|
|
*/ |
90
|
|
|
private function setInstance(QueryInterface $instance) |
91
|
|
|
{ |
92
|
|
|
$this->_instance = $instance; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get database instance |
97
|
|
|
* |
98
|
|
|
* @return QueryInterface |
99
|
|
|
*/ |
100
|
|
|
public function getInstance(): QueryInterface |
101
|
|
|
{ |
102
|
|
|
return $this->_instance; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function update(array $data, array $where = []) |
106
|
|
|
{ |
107
|
|
|
return $this->getInstance()->update($data, $where); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function delete(array $where) |
111
|
|
|
{ |
112
|
|
|
return $this->getInstance()->delete($where); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function exec(string $query) |
116
|
|
|
{ |
117
|
|
|
return $this->getInstance()->exec($query); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function insert(array $data) |
121
|
|
|
{ |
122
|
|
|
return $this->getInstance()->insert($data); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function select(string $query, array $data = []) |
126
|
|
|
{ |
127
|
|
|
return $this->getInstance()->select($query, $data); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function truncate() |
131
|
|
|
{ |
132
|
|
|
return $this->getInstance()->truncate(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|