1 | <?php |
||
9 | class CDatabaseModel implements \Anax\DI\IInjectionAware |
||
10 | { |
||
11 | use \Anax\DI\TInjectable; |
||
12 | |||
13 | public $id; |
||
14 | |||
15 | |||
16 | /** |
||
17 | * Get the table name. |
||
18 | * |
||
19 | * @return string with the table name. |
||
20 | */ |
||
21 | 5 | public function getSource() |
|
22 | { |
||
23 | 5 | return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1))); |
|
24 | } |
||
25 | |||
26 | /** |
||
27 | * Find and return all. |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | 1 | public function findAll() |
|
32 | { |
||
33 | 1 | $this->db->select() |
|
|
|||
34 | 1 | ->from($this->getSource()); |
|
35 | |||
36 | 1 | $this->db->execute(); |
|
37 | 1 | $this->db->setFetchModeClass(__CLASS__); |
|
38 | 1 | return $this->db->fetchAll(); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get object properties. |
||
43 | * |
||
44 | * @return array with object properties. |
||
45 | */ |
||
46 | 2 | public function getProperties() |
|
47 | { |
||
48 | 2 | $properties = get_object_vars($this); |
|
49 | 2 | unset($properties['di']); |
|
50 | 2 | unset($properties['db']); |
|
51 | |||
52 | 2 | return $properties; |
|
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Find and return specific. |
||
58 | * |
||
59 | * @return this |
||
60 | */ |
||
61 | 1 | public function find($id) |
|
62 | { |
||
63 | 1 | $this->db->select() |
|
64 | 1 | ->from($this->getSource()) |
|
65 | 1 | ->where("id = ?"); |
|
66 | |||
67 | 1 | $this->db->execute([$id]); |
|
68 | 1 | return $this->db->fetchInto($this); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Save current object/row. |
||
73 | * |
||
74 | * @param array $values key/values to save or empty to use object properties. |
||
75 | * |
||
76 | * @return boolean true or false if saving went okey. |
||
77 | */ |
||
78 | 1 | public function save($values = []) |
|
79 | { |
||
80 | |||
81 | 1 | $this->setProperties($values); |
|
82 | 1 | $values = $this->getProperties(); |
|
83 | |||
84 | 1 | if (isset($values['id'])) { |
|
85 | 1 | return $this->update($values); |
|
86 | } else { |
||
87 | return $this->create($values); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Set object properties. |
||
93 | * |
||
94 | * @param array $properties with properties to set. |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | 2 | public function setProperties($properties) |
|
107 | |||
108 | /** |
||
109 | * Create new row. |
||
110 | * |
||
111 | * @param array $values key/values to save. |
||
112 | * |
||
113 | * @return boolean true or false if saving went okey. |
||
114 | */ |
||
115 | public function create($values) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Update row. |
||
135 | * |
||
136 | * @param array $values key/values to save. |
||
137 | * |
||
138 | * @return boolean true or false if saving went okey. |
||
139 | */ |
||
140 | 1 | public function update($values) |
|
159 | |||
160 | /** |
||
161 | * Delete row. |
||
162 | * |
||
163 | * @param integer $id to delete. |
||
164 | * |
||
165 | * @return boolean true or false if deleting went okey. |
||
166 | */ |
||
167 | 1 | public function delete($id) |
|
176 | |||
177 | |||
178 | /** |
||
179 | * Build a select-query. |
||
180 | * |
||
181 | * @param string $columns which columns to select. |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 1 | public function query($columns = '*') |
|
192 | |||
193 | /** |
||
194 | * Build the where part. |
||
195 | * |
||
196 | * @param string $condition for building the where part of the query. |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | 1 | public function where($condition) |
|
206 | |||
207 | /** |
||
208 | * Build the where part. |
||
209 | * |
||
210 | * @param string $condition for building the where part of the query. |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | 1 | public function andWhere($condition) |
|
220 | |||
221 | /** |
||
222 | * Execute the query built. |
||
223 | * |
||
224 | * @param array $params |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | 1 | public function execute($params = []) |
|
235 | |||
236 | } |
||
237 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.