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