Rev 51 | Rev 56 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 51 | Rev 52 | ||
|---|---|---|---|
| 1 | <?php
|
1 | <?php
|
| 2 | 2 | ||
| 3 | namespace PointedEars\PHPX\Db; |
3 | namespace PointedEars\PHPX\Db; |
| 4 | 4 | ||
| 5 | require_once __DIR__ . '/../Application.php'; |
- | |
| 6 | require_once __DIR__ . '/../AbstractModel.php'; |
- | |
| 7 | - | ||
| 8 | use \PointedEars\PHPX\Application; |
5 | use \PointedEars\PHPX\Application; |
| 9 | 6 | ||
| 10 | /**
|
7 | /**
|
| 11 | * Generic database table model class
|
8 | * Generic database table model class
|
| 12 | *
|
9 | *
|
| 13 | * @author Thomas Lahn
|
10 | * @author Thomas Lahn
|
| 14 | * @property Database $database
|
11 | * @property Database $database
|
| 15 | * @property-read int $lastInsertId
|
12 | * @property-read int $lastInsertId
|
| 16 | * ID of the last inserted row, or the last value from
|
13 | * ID of the last inserted row, or the last value from
|
| 17 | a sequence object, depending on the underlying driver.
|
14 | a sequence object, depending on the underlying driver.
|
| 18 | */
|
15 | */
|
| 19 | class Table extends \PointedEars\PHPX\AbstractModel |
16 | class Table extends \PointedEars\PHPX\AbstractModel |
| 20 | {
|
17 | {
|
| 21 | /**
|
18 | /**
|
| 22 | * Name of the table
|
19 | * Name of the table
|
| 23 | */
|
20 | */
|
| 24 | protected $_name = ''; |
21 | protected $_name = ''; |
| 25 | 22 | ||
| 26 | /**
|
23 | /**
|
| 27 | * Database of the table
|
24 | * Database of the table
|
| 28 | * @var Database|string
|
25 | * @var Database|string
|
| 29 | */
|
26 | */
|
| 30 | protected $_database; |
27 | protected $_database; |
| 31 | 28 | ||
| 32 | protected $_id = 'id'; |
29 | protected $_id = 'id'; |
| 33 | 30 | ||
| 34 | /**
|
31 | /**
|
| 35 | * Creates a new <code>Table</code> instance.
|
32 | * Creates a new <code>Table</code> instance.
|
| 36 | *
|
33 | *
|
| 37 | * Each of the parameters is optional and can also be given
|
34 | * Each of the parameters is optional and can also be given
|
| 38 | * by a protected property where the parameter name is preceded
|
35 | * by a protected property where the parameter name is preceded
|
| 39 | * by <code>_</code>. Parameter values overwrite the default
|
36 | * by <code>_</code>. Parameter values overwrite the default
|
| 40 | * property values. It is recommended to use default property
|
37 | * property values. It is recommended to use default property
|
| 41 | * values of inheriting classes except for small applications
|
38 | * values of inheriting classes except for small applications
|
| 42 | * and testing purposes.
|
39 | * and testing purposes.
|
| 43 | *
|
40 | *
|
| 44 | * @param Database $database
|
41 | * @param Database $database
|
| 45 | * Database of the table (required in order to use a fitting
|
42 | * Database of the table (required in order to use a fitting
|
| 46 | * query language)
|
43 | * query language)
|
| 47 | * @param string $name
|
44 | * @param string $name
|
| 48 | * Table name
|
45 | * Table name
|
| 49 | * @param string $id
|
46 | * @param string $id
|
| 50 | * Name of the primary key column
|
47 | * Name of the primary key column
|
| 51 | * @throws InvalidArgumentException
|
48 | * @throws InvalidArgumentException
|
| 52 | */
|
49 | */
|
| 53 | public function __construct($database = null, $name = '', $id = '') |
50 | public function __construct($database = null, $name = '', $id = '') |
| 54 | {
|
51 | {
|
| 55 | if ($database === null) |
52 | if ($database === null) |
| 56 | {
|
53 | {
|
| 57 | /* Call getter to convert to Database if possible */
|
54 | /* Call getter to convert to Database if possible */
|
| 58 | if ($this->database === null) |
55 | if ($this->database === null) |
| 59 | {
|
56 | {
|
| 60 | $this->_database = Application::getInstance()->getDefaultDatabase(); |
57 | $this->_database = Application::getInstance()->getDefaultDatabase(); |
| 61 | }
|
58 | }
|
| 62 | }
|
59 | }
|
| 63 | else
|
60 | else
|
| 64 | {
|
61 | {
|
| 65 | $this->_database = $database; |
62 | $this->_database = $database; |
| 66 | }
|
63 | }
|
| 67 | 64 | ||
| 68 | if ($name !== '') |
65 | if ($name !== '') |
| 69 | {
|
66 | {
|
| 70 | $this->_name = $name; |
67 | $this->_name = $name; |
| 71 | }
|
68 | }
|
| 72 | 69 | ||
| 73 | if (!\is_string($this->_name)) |
70 | if (!\is_string($this->_name)) |
| 74 | {
|
71 | {
|
| 75 | throw new \InvalidArgumentException( |
72 | throw new \InvalidArgumentException( |
| 76 | 'Expected string for table name, saw '
|
73 | 'Expected string for table name, saw '
|
| 77 | . \get_class($this->_name) || \gettype($this->_name)); |
74 | . \get_class($this->_name) || \gettype($this->_name)); |
| 78 | }
|
75 | }
|
| 79 | 76 | ||
| 80 | if ($id !== '') |
77 | if ($id !== '') |
| 81 | {
|
78 | {
|
| 82 | $this->_id = $id; |
79 | $this->_id = $id; |
| 83 | }
|
80 | }
|
| 84 | }
|
81 | }
|
| 85 | 82 | ||
| 86 | /**
|
83 | /**
|
| 87 | * Returns the database for the table
|
84 | * Returns the database for the table
|
| 88 | * @return Database
|
85 | * @return Database
|
| 89 | */
|
86 | */
|
| 90 | public function getDatabase() |
87 | public function getDatabase() |
| 91 | {
|
88 | {
|
| 92 | if (\is_string($this->_database)) |
89 | if (\is_string($this->_database)) |
| 93 | {
|
90 | {
|
| 94 | /* Call setter to convert to Database */
|
91 | /* Call setter to convert to Database */
|
| 95 | $this->database = $this->_database; |
92 | $this->database = $this->_database; |
| 96 | }
|
93 | }
|
| 97 | 94 | ||
| 98 | return $this->_database; |
95 | return $this->_database; |
| 99 | }
|
96 | }
|
| 100 | 97 | ||
| 101 | /**
|
98 | /**
|
| 102 | * @param Database|string $value
|
99 | * @param Database|string $value
|
| 103 | * @throws InvalidArgumentException
|
100 | * @throws InvalidArgumentException
|
| 104 | */
|
101 | */
|
| 105 | public function setDatabase ($value) |
102 | public function setDatabase ($value) |
| 106 | {
|
103 | {
|
| 107 | if ($value instanceof Database) |
104 | if ($value instanceof Database) |
| 108 | {
|
105 | {
|
| 109 | $this->_database = $value; |
106 | $this->_database = $value; |
| 110 | }
|
107 | }
|
| 111 | else if ($value !== null) |
108 | else if ($value !== null) |
| 112 | {
|
109 | {
|
| 113 | $database = new $value(); |
110 | $database = new $value(); |
| 114 | if (!($database instanceof Database)) |
111 | if (!($database instanceof Database)) |
| 115 | {
|
112 | {
|
| 116 | throw new \InvalidArgumentException( |
113 | throw new \InvalidArgumentException( |
| 117 | 'Expected Database instance or string for class name, saw '
|
114 | 'Expected Database instance or string for class name, saw '
|
| 118 | . (\get_class($value) || \gettype($value)) |
115 | . (\get_class($value) || \gettype($value)) |
| 119 | ); |
116 | ); |
| 120 | }
|
117 | }
|
| 121 | 118 | ||
| 122 | $this->_database = $database; |
119 | $this->_database = $database; |
| 123 | }
|
120 | }
|
| 124 | }
|
121 | }
|
| 125 | 122 | ||
| 126 | /**
|
123 | /**
|
| 127 | * Initiates a transaction
|
124 | * Initiates a transaction
|
| 128 | *
|
125 | *
|
| 129 | * @return bool
|
126 | * @return bool
|
| 130 | * @see Database::beginTransaction()
|
127 | * @see Database::beginTransaction()
|
| 131 | */
|
128 | */
|
| 132 | public function beginTransaction() |
129 | public function beginTransaction() |
| 133 | {
|
130 | {
|
| 134 | return $this->_database->beginTransaction(); |
131 | return $this->_database->beginTransaction(); |
| 135 | }
|
132 | }
|
| 136 | 133 | ||
| 137 | /**
|
134 | /**
|
| 138 | * Rolls back a transaction
|
135 | * Rolls back a transaction
|
| 139 | *
|
136 | *
|
| 140 | * @return bool
|
137 | * @return bool
|
| 141 | * @see Database::rollBack()
|
138 | * @see Database::rollBack()
|
| 142 | */
|
139 | */
|
| 143 | public function rollBack() |
140 | public function rollBack() |
| 144 | {
|
141 | {
|
| 145 | return $this->_database->rollBack(); |
142 | return $this->_database->rollBack(); |
| 146 | }
|
143 | }
|
| 147 | 144 | ||
| 148 | /**
|
145 | /**
|
| 149 | * Commits a transaction
|
146 | * Commits a transaction
|
| 150 | *
|
147 | *
|
| 151 | * @return bool
|
148 | * @return bool
|
| 152 | * @see Database::commit()
|
149 | * @see Database::commit()
|
| 153 | */
|
150 | */
|
| 154 | public function commit() |
151 | public function commit() |
| 155 | {
|
152 | {
|
| 156 | return $this->_database->commit(); |
153 | return $this->_database->commit(); |
| 157 | }
|
154 | }
|
| 158 | 155 | ||
| 159 | /**
|
156 | /**
|
| 160 | * Retrieves all rows from the table
|
157 | * Retrieves all rows from the table
|
| 161 | *
|
158 | *
|
| 162 | * @return array
|
159 | * @return array
|
| 163 | * @see Database::fetchAll()
|
160 | * @see Database::fetchAll()
|
| 164 | */
|
161 | */
|
| 165 | public function fetchAll($fetch_style = null, $column_index = null, array $ctor_args = null) |
162 | public function fetchAll($fetch_style = null, $column_index = null, array $ctor_args = null) |
| 166 | {
|
163 | {
|
| 167 | return $this->_database->fetchAll($this->_name, $fetch_style, $column_index, $ctor_args); |
164 | return $this->_database->fetchAll($this->_name, $fetch_style, $column_index, $ctor_args); |
| 168 | }
|
165 | }
|
| 169 | 166 | ||
| 170 | /**
|
167 | /**
|
| 171 | * Selects data from one or more tables
|
168 | * Selects data from one or more tables
|
| 172 | *
|
169 | *
|
| 173 | * @return array
|
170 | * @return array
|
| 174 | * @see Database::select()
|
171 | * @see Database::select()
|
| 175 | */
|
172 | */
|
| 176 | public function select($columns = null, $where = null, $order = null, $limit = null) |
173 | public function select($columns = null, $where = null, $order = null, $limit = null) |
| 177 | {
|
174 | {
|
| 178 | return $this->_database->select($this->_name, $columns, $where, $order, $limit); |
175 | return $this->_database->select($this->_name, $columns, $where, $order, $limit); |
| 179 | }
|
176 | }
|
| 180 | 177 | ||
| 181 | /**
|
178 | /**
|
| 182 | * Updates records in one or more tables
|
179 | * Updates records in one or more tables
|
| 183 | *
|
180 | *
|
| 184 | * @return bool
|
181 | * @return bool
|
| 185 | * @see Database::update()
|
182 | * @see Database::update()
|
| 186 | */
|
183 | */
|
| 187 | public function update($data, $condition) |
184 | public function update($data, $condition) |
| 188 | {
|
185 | {
|
| 189 | return $this->_database->update($this->_name, $data, $condition); |
186 | return $this->_database->update($this->_name, $data, $condition); |
| 190 | }
|
187 | }
|
| 191 | 188 | ||
| 192 | /**
|
189 | /**
|
| 193 | * Inserts a record into the table
|
190 | * Inserts a record into the table
|
| 194 | *
|
191 | *
|
| 195 | * @return bool
|
192 | * @return bool
|
| 196 | * @see Database::insert()
|
193 | * @see Database::insert()
|
| 197 | */
|
194 | */
|
| 198 | public function insert($data, $cols = null) |
195 | public function insert($data, $cols = null) |
| 199 | {
|
196 | {
|
| 200 | return $this->_database->insert($this->_name, $data, $cols); |
197 | return $this->_database->insert($this->_name, $data, $cols); |
| 201 | }
|
198 | }
|
| 202 | 199 | ||
| 203 | /**
|
200 | /**
|
| 204 | * Returns the ID of the last inserted row, or the last value from
|
201 | * Returns the ID of the last inserted row, or the last value from
|
| 205 | * a sequence object, depending on the underlying driver.
|
202 | * a sequence object, depending on the underlying driver.
|
| 206 | *
|
203 | *
|
| 207 | * @return int
|
204 | * @return int
|
| 208 | * @see Database::getLastInsertId()
|
205 | * @see Database::getLastInsertId()
|
| 209 | */
|
206 | */
|
| 210 | public function getLastInsertId() |
207 | public function getLastInsertId() |
| 211 | {
|
208 | {
|
| 212 | return $this->_database->lastInsertId; |
209 | return $this->_database->lastInsertId; |
| 213 | }
|
210 | }
|
| 214 | 211 | ||
| 215 | /**
|
212 | /**
|
| 216 | * Delete a record from the table
|
213 | * Delete a record from the table
|
| 217 | *
|
214 | *
|
| 218 | * @param int $id
|
215 | * @param int $id
|
| 219 | * ID of the record to delete. May be <code>null</code>,
|
216 | * ID of the record to delete. May be <code>null</code>,
|
| 220 | * in which case <var>$condition</var> must specify
|
217 | * in which case <var>$condition</var> must specify
|
| 221 | * the records to be deleted.
|
218 | * the records to be deleted.
|
| 222 | * @param array[optional] $condition
|
219 | * @param array[optional] $condition
|
| 223 | * Conditions that must be met for a record to be deleted.
|
220 | * Conditions that must be met for a record to be deleted.
|
| 224 | * Ignored if <var>$id</var> is not <code>null</code>.
|
221 | * Ignored if <var>$id</var> is not <code>null</code>.
|
| 225 | * @return bool
|
222 | * @return bool
|
| 226 | * @throws InvalidArgumentException if both <var>$id</var> and
|
223 | * @throws InvalidArgumentException if both <var>$id</var> and
|
| 227 | * <var>$condition</var> are <code>null</code>.
|
224 | * <var>$condition</var> are <code>null</code>.
|
| 228 | * @see Database::delete()
|
225 | * @see Database::delete()
|
| 229 | */
|
226 | */
|
| 230 | public function delete ($id, array $condition = null) |
227 | public function delete ($id, array $condition = null) |
| 231 | {
|
228 | {
|
| 232 | if ($id !== null) |
229 | if ($id !== null) |
| 233 | {
|
230 | {
|
| 234 | $condition = array($this->_id => $id); |
231 | $condition = array($this->_id => $id); |
| 235 | }
|
232 | }
|
| 236 | else if ($condition === null) |
233 | else if ($condition === null) |
| 237 | {
|
234 | {
|
| 238 | throw new InvalidArgumentException( |
235 | throw new InvalidArgumentException( |
| 239 | '$id and $condition cannot both be null'); |
236 | '$id and $condition cannot both be null'); |
| 240 | }
|
237 | }
|
| 241 | 238 | ||
| 242 | return $this->_database->delete($this->_name, $condition); |
239 | return $this->_database->delete($this->_name, $condition); |
| 243 | }
|
240 | }
|
| 244 | 241 | ||
| 245 | /**
|
242 | /**
|
| 246 | * Inserts a row into the table or updates an existing one
|
243 | * Inserts a row into the table or updates an existing one
|
| 247 | *
|
244 | *
|
| 248 | * @param array $data
|
245 | * @param array $data
|
| 249 | * Associative array of column-value pairs to be updated/inserted
|
246 | * Associative array of column-value pairs to be updated/inserted
|
| 250 | * @param string|array $condition
|
247 | * @param string|array $condition
|
| 251 | * If there are no records matching this condition, a row will be inserted;
|
248 | * If there are no records matching this condition, a row will be inserted;
|
| 252 | * otherwise matching records are updated
|
249 | * otherwise matching records are updated
|
| 253 | * @return bool
|
250 | * @return bool
|
| 254 | * @see Table::update()
|
251 | * @see Table::update()
|
| 255 | * @see Table::insert()
|
252 | * @see Table::insert()
|
| 256 | */
|
253 | */
|
| 257 | public function updateOrInsert($data, array $condition = null) |
254 | public function updateOrInsert($data, array $condition = null) |
| 258 | {
|
255 | {
|
| 259 | if ($this->select($this->_id, $condition)) |
256 | if ($this->select($this->_id, $condition)) |
| 260 | {
|
257 | {
|
| 261 | return $this->update($data, $condition); |
258 | return $this->update($data, $condition); |
| 262 | }
|
259 | }
|
| 263 | 260 | ||
| 264 | return $this->insert($data); |
261 | return $this->insert($data); |
| 265 | }
|
262 | }
|
| 266 | 263 | ||
| 267 | /**
|
264 | /**
|
| 268 | * Finds a record by ID
|
265 | * Finds a record by ID
|
| 269 | *
|
266 | *
|
| 270 | * @param mixed $id
|
267 | * @param mixed $id
|
| 271 | * @return array
|
268 | * @return array
|
| 272 | */
|
269 | */
|
| 273 | public function find ($id) |
270 | public function find ($id) |
| 274 | {
|
271 | {
|
| 275 | /* DEBUG */
|
272 | /* DEBUG */
|
| 276 | if (defined('DEBUG') && DEBUG > 0) |
273 | if (defined('DEBUG') && DEBUG > 0) |
| 277 | {
|
274 | {
|
| 278 | debug($id); |
275 | debug($id); |
| 279 | }
|
276 | }
|
| 280 | 277 | ||
| 281 | $result = $this->select(null, array($this->_id => $id)); |
278 | $result = $this->select(null, array($this->_id => $id)); |
| 282 | 279 | ||
| 283 | if ($result) |
280 | if ($result) |
| 284 | {
|
281 | {
|
| 285 | $result = $result[0]; |
282 | $result = $result[0]; |
| 286 | }
|
283 | }
|
| 287 | 284 | ||
| 288 | return $result; |
285 | return $result; |
| 289 | }
|
286 | }
|
| 290 | }
|
287 | }
|