1 | <?php |
||
12 | class ActiveRecordModel |
||
13 | { |
||
14 | /** |
||
15 | * @var DatabaseQueryBuilder $db the object for persistent |
||
16 | * storage. |
||
17 | */ |
||
18 | protected $db = null; |
||
19 | |||
20 | /** |
||
21 | * @var string $tableName name of the database table. |
||
22 | */ |
||
23 | protected $tableName = null; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * Set the database object to use for accessing storage. |
||
29 | * |
||
30 | * @param DatabaseQueryBuilder $db as database access object. |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | 3 | public function setDb(DatabaseQueryBuilder $db) |
|
38 | |||
39 | |||
40 | |||
41 | /** |
||
42 | * Check if database is injected or throw an exception. |
||
43 | * |
||
44 | * @throws ActiveRecordException when database is not set. |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | 3 | protected function checkDb() |
|
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * Get essential object properties. |
||
59 | * |
||
60 | * @return array with object properties. |
||
61 | */ |
||
62 | 3 | protected function getProperties() |
|
63 | { |
||
64 | 3 | $properties = get_object_vars($this); |
|
65 | 3 | unset($properties['tableName']); |
|
66 | 3 | unset($properties['db']); |
|
67 | 3 | unset($properties['di']); |
|
68 | 3 | return $properties; |
|
69 | } |
||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * Find and return first object found by search criteria and use |
||
75 | * its data to populate this instance. |
||
76 | * |
||
77 | * @param string $column to use in where statement. |
||
78 | * @param mixed $value to use in where statement. |
||
79 | * |
||
80 | * @return this |
||
81 | */ |
||
82 | 2 | public function find($column, $value) |
|
83 | { |
||
84 | 2 | $this->checkDb(); |
|
85 | 2 | return $this->db->connect() |
|
86 | 2 | ->select() |
|
87 | 2 | ->from($this->tableName) |
|
88 | 2 | ->where("$column = ?") |
|
89 | 2 | ->execute([$value]) |
|
|
|||
90 | 2 | ->fetchInto($this); |
|
91 | } |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * Find and return all. |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | 1 | public function findAll() |
|
101 | { |
||
102 | 1 | $this->checkDb(); |
|
103 | 1 | return $this->db->connect() |
|
104 | 1 | ->select() |
|
105 | 1 | ->from($this->tableName) |
|
106 | 1 | ->execute() |
|
107 | 1 | ->fetchAllClass(get_class($this)); |
|
108 | } |
||
109 | |||
110 | |||
111 | |||
112 | /** |
||
113 | * Find and return all matching a search criteria of |
||
114 | * for example `id = ?` or `id IN [?, ?]`. |
||
115 | * |
||
116 | * @param string $where to use in where statement. |
||
117 | * @param mixed $value to use in where statement. |
||
118 | * |
||
119 | * @return array of object of this class |
||
120 | */ |
||
121 | public function findAllWhere($where, $value) |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Execute rawsql |
||
136 | * @param string $sql rawsql |
||
137 | * @param array $params params |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function findAllSql($sql, $params) |
||
142 | { |
||
143 | $this->checkDb(); |
||
144 | return $this->db->connect() |
||
145 | ->execute($sql, $params) |
||
146 | ->fetchAllClass(get_class($this)); |
||
147 | } |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Save current object/row, insert if id is missing and do an |
||
153 | * update if the id exists. |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | 3 | public function save($idName = null, $id = null) |
|
158 | { |
||
159 | 3 | if (isset($this->id)) { |
|
160 | return $this->update(); |
||
161 | 3 | } elseif ($idName !== null) { |
|
162 | return $this->update($idName, $id); |
||
163 | } |
||
164 | |||
165 | 3 | return $this->create(); |
|
166 | } |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * Create new row. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | 3 | protected function create() |
|
176 | { |
||
177 | 3 | $this->checkDb(); |
|
178 | 3 | $properties = $this->getProperties(); |
|
179 | 3 | unset($properties['id']); |
|
180 | 3 | $columns = array_keys($properties); |
|
181 | 3 | $values = array_values($properties); |
|
182 | |||
183 | 3 | $this->db->connect() |
|
184 | 3 | ->insert($this->tableName, $columns) |
|
185 | 3 | ->execute($values); |
|
186 | |||
187 | 3 | $this->id = $this->db->lastInsertId(); |
|
188 | 3 | } |
|
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * Update row. |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | protected function update($idName = null, $id = null) |
||
212 | |||
213 | |||
214 | |||
215 | |||
216 | |||
217 | |||
218 | /** |
||
219 | * Delete row. |
||
220 | * |
||
221 | * @param integer $id to delete or use $this->id as default. |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | public function delete($idName = null, $id = null) |
||
238 | } |
||
239 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: