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