| @@ -136,8 +136,10 @@ | ||
| 136 | 136 | */ |
| 137 | 137 | public function __construct($dbDriver, $table, $key = array('id')) { |
| 138 | 138 | // Hookable! |
| 139 | 139 | parent::__construct(); |
| 140 | + // Reset intenal item object! | |
| 141 | + $this->setItem(); | |
| 140 | 142 | // Initialize! |
| 141 | 143 | $this->dbDriver =$dbDriver; |
| 142 | 144 | $this->name = "#__cjtoolbox_{$table}"; |
| 143 | 145 | $this->key = $key; |
| @@ -167,12 +169,25 @@ | ||
| 167 | 169 | |
| 168 | 170 | /** |
| 169 | 171 | * put your comment there... |
| 170 | 172 | * |
| 173 | + */ | |
| 174 | + public function fetchAll() { | |
| 175 | + // Get query. | |
| 176 | + $query = $this->getLoadQuery(); | |
| 177 | + // Return all result. | |
| 178 | + return $this->dbDriver->select($query, ARRAY_A); | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * put your comment there... | |
| 183 | + * | |
| 171 | 184 | * @param mixed $field |
| 172 | 185 | */ |
| 173 | 186 | public function get($field) { |
| 174 | - return $this->ongetfield($this->item->{$field}, $field); | |
| 187 | + // Get the value of the field with E_ALL complain! | |
| 188 | + $value = (is_object($this->item) && property_exists($this->item, $field)) ? $this->item->{$field} : null; | |
| 189 | + return $this->ongetfield($value, $field); | |
| 175 | 190 | } |
| 176 | 191 | |
| 177 | 192 | /** |
| 178 | 193 | * put your comment there... |
| @@ -177,9 +192,9 @@ | ||
| 177 | 192 | /** |
| 178 | 193 | * put your comment there... |
| 179 | 194 | * |
| 180 | 195 | */ |
| 181 | - public function &getData() { | |
| 196 | + public function getData() { | |
| 182 | 197 | return $this->ongetdata($this->item); |
| 183 | 198 | } |
| 184 | 199 | |
| 185 | 200 | /** |
| @@ -208,8 +223,40 @@ | ||
| 208 | 223 | |
| 209 | 224 | /** |
| 210 | 225 | * put your comment there... |
| 211 | 226 | * |
| 227 | + * @param mixed $query | |
| 228 | + */ | |
| 229 | + protected function getLoadQuery($query = null) { | |
| 230 | + $tableKey = null; | |
| 231 | + $key = null; | |
| 232 | + // Query might be an array of keys! | |
| 233 | + if (is_array($query)) { | |
| 234 | + $tableKey = $query; | |
| 235 | + $query = null; | |
| 236 | + } | |
| 237 | + if (!$query) { | |
| 238 | + $item = (array) $this->item; | |
| 239 | + $query['select'] = 'SELECT *'; | |
| 240 | + $query['from'] = "FROM {$this->table()}"; | |
| 241 | + // Load only if key is not NULL! | |
| 242 | + if ($key = $this->isValidKey($tableKey)) { | |
| 243 | + $query['where'] = 'WHERE ' . implode(' AND ', $this->prepareQueryParameters($key)); | |
| 244 | + if ($query = $this->onconcatquery($query)) { | |
| 245 | + // Read DB record! | |
| 246 | + $query = "{$query['select']} {$query['from']} {$query['where']}"; | |
| 247 | + } | |
| 248 | + } | |
| 249 | + else { | |
| 250 | + throw new CJT_Framework_DB_Mysql_Driver_Exception_Invalidkey($key); | |
| 251 | + } | |
| 252 | + } | |
| 253 | + return $query; | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * put your comment there... | |
| 258 | + * | |
| 212 | 259 | * @param mixed $tableKey |
| 213 | 260 | */ |
| 214 | 261 | public function getKey($tableKey = null) { |
| 215 | 262 | if (!$tableKey) { |
| @@ -257,8 +304,10 @@ | ||
| 257 | 304 | } |
| 258 | 305 | return $isValid; |
| 259 | 306 | } |
| 260 | 307 | |
| 308 | + | |
| 309 | + | |
| 261 | 310 | /** |
| 262 | 311 | * Load record into table! |
| 263 | 312 | * |
| 264 | 313 | * @param mixed |
| @@ -263,31 +312,19 @@ | ||
| 263 | 312 | * |
| 264 | 313 | * @param mixed |
| 265 | 314 | */ |
| 266 | 315 | public function load($query = null) { |
| 267 | - $key = null; | |
| 268 | - // Query might be an array of keys! | |
| 269 | - if (is_array($query)) { | |
| 270 | - $tableKey = $query; | |
| 271 | - $query = null; | |
| 316 | + try { | |
| 317 | + // Get query to load! | |
| 318 | + $query = $this->getLoadQuery($query); | |
| 319 | + // Set the returned item. | |
| 320 | + $this->setItem($this->dbDriver->getRow($this->onloadquery($query))); | |
| 272 | 321 | } |
| 273 | - if (!$query) { | |
| 274 | - $item = (array) $this->item; | |
| 275 | - $query['select'] = 'SELECT *'; | |
| 276 | - $query['from'] = "FROM {$this->table()}"; | |
| 277 | - // Load only if key is not NULL! | |
| 278 | - if ($key = $this->isValidKey($tableKey)) { | |
| 279 | - $query['where'] = 'WHERE ' . implode(' AND ', $this->prepareQueryParameters($key)); | |
| 280 | - if ($query = $this->onconcatquery($query)) { | |
| 281 | - // Read DB record! | |
| 282 | - $query = "{$query['select']} {$query['from']} {$query['where']}"; | |
| 283 | - $this->item = array_shift($this->dbDriver->select($this->onloadquery($query))); | |
| 284 | - } | |
| 285 | - } | |
| 322 | + catch (CJT_Framework_DB_Mysql_Driver_Exception_Invalidkey $exception) { | |
| 323 | + // Backword compatibility to don't show any error. | |
| 324 | + // This is how the old code procedure was! | |
| 286 | 325 | } |
| 287 | - else { | |
| 288 | - $this->item = array_shift($this->dbDriver->select($this->onloadquery($query))); | |
| 289 | - } | |
| 326 | + // Chaining. | |
| 290 | 327 | return $this; |
| 291 | 328 | } |
| 292 | 329 | |
| 293 | 330 | /** |
| @@ -318,32 +355,21 @@ | ||
| 318 | 355 | return $prepared; |
| 319 | 356 | } |
| 320 | 357 | |
| 321 | 358 | /** |
| 322 | - * put your comment there... | |
| 323 | - * | |
| 324 | - */ | |
| 325 | - public function reset() { | |
| 326 | - $this->item = null; | |
| 327 | - return $this; | |
| 328 | - } | |
| 329 | - | |
| 330 | - /** | |
| 331 | - * UPDATE/INSERT | |
| 332 | - * | |
| 333 | 359 | * THIS METHOD STILL DOESNT SUPPORT COMPOUND KEYS!! |
| 334 | 360 | * |
| 335 | 361 | * @param mixed $forceInsert |
| 362 | + * @param mixed $updateIdField | |
| 336 | 363 | * @return CJTxTable |
| 337 | 364 | */ |
| 338 | - public function save($forceInsert = false) { | |
| 365 | + public function save($forceInsert = false, $updateIdField = false) { | |
| 339 | 366 | $keyFieldName = $this->key[0]; |
| 340 | - $id = $this->item->{$keyFieldName}; | |
| 341 | 367 | $item = (array) $this->item; |
| 342 | - // Don't update id field. | |
| 343 | - $fieldsList = array_diff_key($item, array_flip($this->key)); | |
| 368 | + // Don't update id field unless $updateIdField is set to TRUE. | |
| 369 | + $fieldsList = !$updateIdField ? array_diff_key($item, array_flip($this->key)) : $item; | |
| 344 | 370 | $fieldsList = implode(',', $this->prepareQueryParameters($fieldsList)); |
| 345 | - if (!$forceInsert && $id) { // Update | |
| 371 | + if (!$forceInsert && $this->get($keyFieldName)) { // Update | |
| 346 | 372 | // Where clause. |
| 347 | 373 | $condition = implode(' AND ', $this->prepareQueryParameters($this->getKey())); |
| 348 | 374 | $query = "UPDATE {$this->table()} SET {$fieldsList} WHERE {$condition}"; |
| 349 | 375 | if ($query = $this->onupdate($query)) { |
| @@ -383,11 +409,8 @@ | ||
| 383 | 409 | */ |
| 384 | 410 | public function setData($data) { |
| 385 | 411 | // Cast to array. |
| 386 | 412 | $data = (array) $data; |
| 387 | - if (is_null($this->item)) { | |
| 388 | - $item = (object) array(); | |
| 389 | - } | |
| 390 | 413 | // Copy values! |
| 391 | 414 | foreach ($data as $name => $value) { |
| 392 | 415 | if ($value !== null) { |
| 393 | 416 | $this->set($name, $value); |
| @@ -395,8 +418,25 @@ | ||
| 395 | 418 | } |
| 396 | 419 | return $this; |
| 397 | 420 | } |
| 398 | 421 | |
| 422 | + /** | |
| 423 | + * put your comment there... | |
| 424 | + * | |
| 425 | + * @param mixed $item | |
| 426 | + * @return CJTxTable | |
| 427 | + */ | |
| 428 | + public function setItem($item = null) { | |
| 429 | + // If NULL create empty stdClass, if array cast to stdClass! | |
| 430 | + if (!is_object($item)) { | |
| 431 | + $item = is_array($item) ? (object) $item : new stdClass(); | |
| 432 | + } | |
| 433 | + // Set internal item object! | |
| 434 | + $this->item = $item; | |
| 435 | + // Chaining! | |
| 436 | + return $this; | |
| 437 | + } | |
| 438 | + | |
| 399 | 439 | /** |
| 400 | 440 | * put your comment there... |
| 401 | 441 | * |
| 402 | 442 | * @param mixed $key |