|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Describe\Describe; |
|
6
|
|
|
use Rougin\SparkPlug\Instance; |
|
7
|
|
|
use Rougin\Describe\Driver\CodeIgniterDriver; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Wildfire |
|
11
|
|
|
* |
|
12
|
|
|
* Yet another wrapper for CodeIgniter's Query Builder Class. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Wildfire |
|
15
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wildfire |
|
18
|
|
|
{ |
|
19
|
|
|
use ResultTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var CI_DB |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $db; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Rougin\Describe\Describe |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $describe; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CI_DB |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $query; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $table = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param CI_DB|null $database |
|
43
|
|
|
* @param CI_DB_result|null $query |
|
44
|
|
|
*/ |
|
45
|
18 |
|
public function __construct($database = null, $query = null) |
|
46
|
|
|
{ |
|
47
|
18 |
|
$config = []; |
|
48
|
|
|
|
|
49
|
18 |
|
if (empty($database)) { |
|
50
|
3 |
|
$ci = Instance::create(); |
|
51
|
|
|
|
|
52
|
3 |
|
$ci->load->database(); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
3 |
|
$database = $ci->db; |
|
|
|
|
|
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
18 |
|
$this->db = $database; |
|
58
|
18 |
|
$this->query = $query; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
18 |
|
$config['default'] = [ |
|
61
|
18 |
|
'dbdriver' => $database->dbdriver, |
|
62
|
18 |
|
'hostname' => $database->hostname, |
|
63
|
18 |
|
'username' => $database->username, |
|
64
|
18 |
|
'password' => $database->password, |
|
65
|
18 |
|
'database' => $database->database |
|
66
|
18 |
|
]; |
|
67
|
|
|
|
|
68
|
18 |
|
if (empty($config['default']['hostname'])) { |
|
69
|
18 |
|
$config['default']['hostname'] = $database->dsn; |
|
70
|
18 |
|
} |
|
71
|
|
|
|
|
72
|
18 |
|
$driver = new CodeIgniterDriver($config); |
|
73
|
18 |
|
$this->describe = new Describe($driver); |
|
74
|
18 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
78
|
|
|
* the specified table. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $table |
|
81
|
|
|
* @param array|integer $delimiters |
|
82
|
|
|
* @return object|boolean |
|
83
|
|
|
*/ |
|
84
|
15 |
|
public function find($table, $delimiters = []) |
|
85
|
|
|
{ |
|
86
|
15 |
|
if ( ! is_array($delimiters)) { |
|
87
|
6 |
|
$primaryKey = $this->describe->getPrimaryKey($table); |
|
88
|
|
|
|
|
89
|
6 |
|
$delimiters = [ $primaryKey => $delimiters ]; |
|
90
|
6 |
|
} |
|
91
|
|
|
|
|
92
|
15 |
|
$this->db->where($delimiters); |
|
93
|
|
|
|
|
94
|
15 |
|
$query = $this->db->get($table); |
|
95
|
|
|
|
|
96
|
15 |
|
if ($query->num_rows() > 0) { |
|
97
|
12 |
|
return $this->createObject($table, $query->row()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return all rows from the specified table. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $table |
|
107
|
|
|
* @return self |
|
108
|
|
|
*/ |
|
109
|
12 |
|
public function get($table = '') |
|
110
|
|
|
{ |
|
111
|
12 |
|
if ($this->query == null) { |
|
112
|
9 |
|
$this->query = $this->db->get($table); |
|
113
|
9 |
|
} |
|
114
|
|
|
|
|
115
|
12 |
|
$this->table = $table; |
|
116
|
|
|
|
|
117
|
|
|
// Guess the specified table from the query |
|
118
|
12 |
|
if (empty($table)) { |
|
119
|
3 |
|
$query = $this->db->last_query(); |
|
120
|
|
|
|
|
121
|
3 |
|
preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches); |
|
122
|
|
|
|
|
123
|
3 |
|
$this->table = $matches[1]; |
|
124
|
3 |
|
} |
|
125
|
|
|
|
|
126
|
12 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sets the database class. |
|
131
|
|
|
* |
|
132
|
|
|
* @param CI_DB $database |
|
133
|
|
|
* @return self |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public function setDatabase($database) |
|
136
|
|
|
{ |
|
137
|
3 |
|
$this->db = $database; |
|
138
|
|
|
|
|
139
|
3 |
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Calls methods from this class in underscore case. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $method |
|
146
|
|
|
* @param mixed $parameters |
|
147
|
|
|
* @return mixed |
|
148
|
|
|
*/ |
|
149
|
6 |
|
public function __call($method, $parameters) { |
|
150
|
6 |
|
$method = Inflector::camelize($method); |
|
151
|
6 |
|
$result = $this; |
|
152
|
|
|
|
|
153
|
6 |
|
if (method_exists($this, $method)) { |
|
154
|
6 |
|
$class = [$this, $method]; |
|
155
|
|
|
|
|
156
|
6 |
|
$result = call_user_func_array($class, $parameters); |
|
157
|
6 |
|
} |
|
158
|
|
|
|
|
159
|
6 |
|
return $result; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.