|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Wildfire\Helpers\ModelHelper; |
|
6
|
|
|
use Rougin\Wildfire\Helpers\DescribeHelper; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Wildfire |
|
10
|
|
|
* |
|
11
|
|
|
* Yet another wrapper for CodeIgniter's Query Builder Class. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Wildfire |
|
14
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Wildfire extends \CI_Model |
|
17
|
|
|
{ |
|
18
|
|
|
use Traits\DatabaseTrait, Traits\ObjectTrait, Traits\ResultTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Rougin\Describe\Describe |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $describe; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param \CI_DB|null $database |
|
27
|
|
|
* @param \CI_DB_result|null $query |
|
28
|
|
|
*/ |
|
29
|
39 |
|
public function __construct($database = null, $query = null) |
|
30
|
|
|
{ |
|
31
|
39 |
|
$this->setDatabase($database); |
|
32
|
|
|
|
|
33
|
39 |
|
$this->describe = DescribeHelper::createInstance($this->db); |
|
34
|
39 |
|
$this->query = $query; |
|
35
|
39 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
39
|
|
|
* the specified table. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $tableName |
|
42
|
|
|
* @param array|integer $delimiters |
|
43
|
|
|
* @return object|boolean |
|
44
|
|
|
*/ |
|
45
|
18 |
|
public function find($tableName, $delimiters = []) |
|
46
|
|
|
{ |
|
47
|
18 |
|
list($tableName, $model) = ModelHelper::createInstance($tableName); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
18 |
|
if (is_integer($delimiters)) { |
|
50
|
12 |
|
$primaryKey = $this->describe->getPrimaryKey($tableName); |
|
51
|
|
|
|
|
52
|
12 |
|
$delimiters = [ $primaryKey => $delimiters ]; |
|
53
|
12 |
|
} |
|
54
|
|
|
|
|
55
|
18 |
|
$this->db->where($delimiters); |
|
56
|
|
|
|
|
57
|
18 |
|
$query = $this->db->get($tableName); |
|
58
|
|
|
|
|
59
|
18 |
|
if ($query->num_rows() > 0) { |
|
60
|
12 |
|
return $this->createObject($tableName, $query->row()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns all rows from the specified table. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $table |
|
70
|
|
|
* @return self |
|
71
|
|
|
*/ |
|
72
|
24 |
|
public function get($table = '') |
|
73
|
|
|
{ |
|
74
|
|
|
// Guess the specified table from the query |
|
75
|
24 |
|
if (empty($table)) { |
|
76
|
6 |
|
$query = $this->db->last_query(); |
|
77
|
|
|
|
|
78
|
6 |
|
preg_match('/\bfrom\b\s*(\w+)/i', $query, $matches); |
|
79
|
|
|
|
|
80
|
6 |
|
$this->table = $table = $matches[1]; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
6 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
18 |
|
list($tableName, $model) = ModelHelper::createInstance($table); |
|
86
|
|
|
|
|
87
|
18 |
|
$this->table = $tableName; |
|
88
|
|
|
|
|
89
|
18 |
|
if ($this->query == null) { |
|
90
|
18 |
|
$this->query = $this->db->get($tableName); |
|
91
|
18 |
|
} |
|
92
|
|
|
|
|
93
|
18 |
|
if ($model == $table) { |
|
94
|
6 |
|
$this->table = $model; |
|
95
|
6 |
|
} |
|
96
|
|
|
|
|
97
|
18 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Calls methods from this class in underscore case. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $method |
|
104
|
|
|
* @param mixed $parameters |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
9 |
|
public function __call($method, $parameters) |
|
108
|
|
|
{ |
|
109
|
9 |
|
$method = camelize($method); |
|
110
|
9 |
|
$result = $this; |
|
111
|
|
|
|
|
112
|
9 |
|
if (method_exists($this, $method)) { |
|
113
|
9 |
|
$class = [$this, $method]; |
|
114
|
|
|
|
|
115
|
9 |
|
$result = call_user_func_array($class, $parameters); |
|
116
|
9 |
|
} |
|
117
|
|
|
|
|
118
|
9 |
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.