| 250 | | public function load(array $data) |
| 251 | | { |
| 252 | | foreach ($data as $key => $val) |
| 253 | | { |
| 254 | | // Set each value separately |
| 255 | | $this->__set($key, $val); |
| 256 | | } |
| | 261 | public function load($data, $replace = FALSE) |
| | 262 | { |
| | 263 | // Force the data to be an array |
| | 264 | $data = (array) $data; |
| | 265 | |
| | 266 | if ($replace === TRUE) |
| | 267 | { |
| | 268 | // Empty the data |
| | 269 | $this->__empty_data(); |
| | 270 | |
| | 271 | // Replace the existing data with the loaded data |
| | 272 | $this->data = $data; |
| | 273 | |
| | 274 | // Set types |
| | 275 | $this->__set_types(); |
| | 276 | |
| | 277 | // Object is loaded and saved |
| | 278 | $this->loaded = $this->saved = TRUE; |
| | 279 | } |
| | 280 | else |
| | 281 | { |
| | 282 | foreach ($data as $key => $val) |
| | 283 | { |
| | 284 | // Set each value separately |
| | 285 | $this->__set($key, $val); |
| | 286 | } |
| | 287 | } |
| | 288 | |
| | 289 | return $this; |
| | 290 | } |
| | 291 | |
| | 292 | /** |
| | 293 | * Loads the first result of a statement as a new data set. |
| | 294 | * |
| | 295 | * @param object PDOStatement to execute |
| | 296 | * @return void |
| | 297 | */ |
| | 298 | protected function __load_query(PDOStatement $query) |
| | 299 | { |
| | 300 | // Empty the data |
| | 301 | $this->__empty_data(); |
| | 302 | |
| | 303 | if ($query->execute() AND $query->rowCount() > 0) |
| | 304 | { |
| | 305 | // Load the data of the object |
| | 306 | $this->data = $query->fetch(PDO::FETCH_ASSOC); |
| | 307 | |
| | 308 | // No data has been changed |
| | 309 | $this->changed = array(); |
| | 310 | |
| | 311 | // Data has been loaded and is saved |
| | 312 | $this->loaded = $this->saved = TRUE; |
| | 313 | |
| | 314 | // Reset the types of loaded data |
| | 315 | $this->__set_types(); |
| | 316 | } |
| | 317 | |
| | 318 | // Execute the on_load event |
| | 319 | $this->__on_load(); |
| 311 | | $query = $this->db->prepare('SELECT '.$table.'.* FROM '.$table.' WHERE '.$key.' '.$op.' '.$value.' '.$this->db->limit(1)); |
| 312 | | |
| 313 | | if ($query->execute() AND $query->rowCount() > 0) |
| 314 | | { |
| 315 | | // Load the data of the object |
| 316 | | $this->data = $query->fetch(PDO::FETCH_ASSOC); |
| | 373 | $this->__load_query($this->db->prepare($this->select_sql.' WHERE '.$key.' '.$op.' '.$value.' '.$this->db->limit(1))); |
| | 374 | |
| | 375 | return $this; |
| | 376 | } |
| | 377 | |
| | 378 | /** |
| | 379 | * Saves the current object back into the database. |
| | 380 | * |
| | 381 | * @return boolean |
| | 382 | */ |
| | 383 | public function save() |
| | 384 | { |
| | 385 | if ($this->saved === TRUE) |
| | 386 | return TRUE; |
| | 387 | |
| | 388 | if (is_array($errors = $this->__validate())) |
| | 389 | return $errors; |
| | 390 | |
| | 391 | if ($this->loaded === TRUE) |
| | 392 | { |
| | 393 | // Perform an UPDATE |
| | 394 | $insert = FALSE; |
| | 395 | |
| | 396 | // Create the SQL |
| | 397 | $sql = 'UPDATE '.$this->db->quote_identifier($this->table).' SET '; |
| | 398 | |
| | 399 | $set = array(); |
| | 400 | foreach ($this->changed as $key) |
| | 401 | { |
| | 402 | // Add the new data |
| | 403 | $set[] = $this->db->quote_identifier($key).' = '.$this->db->quote($this->data[$key]); |
| | 404 | } |
| | 405 | |
| | 406 | // Add the WHERE |
| | 407 | $sql .= implode(', ', $set).' WHERE '.$this->db->quote_identifier($this->primary_key).' = '.$this->db->quote($this->data[$this->primary_key]); |
| | 408 | } |
| | 409 | else |
| | 410 | { |
| | 411 | // Perform an INSERT |
| | 412 | $insert = TRUE; |
| | 413 | |
| | 414 | $data = array(); |
| | 415 | foreach ($this->changed as $key) |
| | 416 | { |
| | 417 | // Load the changed data |
| | 418 | $data[$this->db->quote_identifier($key)] = $this->db->quote($this->data[$key]); |
| | 419 | } |
| | 420 | |
| | 421 | if ($this->auto_increment === TRUE) |
| | 422 | { |
| | 423 | // Remove the primary key from the insert |
| | 424 | unset($data[$this->primary_key]); |
| | 425 | } |
| | 426 | |
| | 427 | // Create the SQL statement |
| | 428 | $sql = 'INSERT INTO '.$this->db->quote_identifier($this->table).' ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $data).')'; |
| | 429 | } |
| | 430 | |
| | 431 | if ($count = $this->db->exec($sql)) |
| | 432 | { |
| | 433 | if ($insert === TRUE AND $this->auto_increment === TRUE) |
| | 434 | { |
| | 435 | // Get and assign the insert ID |
| | 436 | $this->data[$this->primary_key] = $this->db->lastInsertId(); |
| | 437 | } |
| 321 | | // Data has been loaded and is saved |
| 322 | | $this->loaded = $this->saved = TRUE; |
| 323 | | |
| 324 | | // Reset the types of loaded data |
| 325 | | $this->__set_types(); |
| 326 | | } |
| 327 | | |
| 328 | | // Execute the on_find event |
| 329 | | $this->__on_find(); |
| 330 | | |
| 331 | | return $this; |
| 332 | | } |
| 333 | | |
| 334 | | /** |
| 335 | | * Saves the current object back into the database. |
| 336 | | * |
| 337 | | * @return boolean |
| 338 | | */ |
| 339 | | public function save() |
| 340 | | { |
| 341 | | if ($this->saved === TRUE) |
| 342 | | return TRUE; |
| 343 | | |
| 344 | | if (is_array($errors = $this->__validate())) |
| 345 | | return $errors; |
| 346 | | |
| 347 | | if ($this->loaded === TRUE) |
| 348 | | { |
| 349 | | // Perform an UPDATE |
| 350 | | $insert = FALSE; |
| 351 | | |
| 352 | | // Create the SQL |
| 353 | | $sql = 'UPDATE '.$this->db->quote_identifier($this->table).' SET '; |
| 354 | | |
| 355 | | $set = array(); |
| 356 | | foreach ($this->changed as $key) |
| 357 | | { |
| 358 | | // Add the new data |
| 359 | | $set[] = $this->db->quote_identifier($key).' = '.$this->db->quote($this->data[$key]); |
| 360 | | } |
| 361 | | |
| 362 | | // Add the WHERE |
| 363 | | $sql .= implode(', ', $set).' WHERE '.$this->db->quote_identifier($this->primary_key).' = '.$this->db->quote($this->data[$this->primary_key]); |
| 364 | | } |
| 365 | | else |
| 366 | | { |
| 367 | | // Perform an INSERT |
| 368 | | $insert = TRUE; |
| 369 | | |
| 370 | | $data = array(); |
| 371 | | foreach ($this->changed as $key) |
| 372 | | { |
| 373 | | // Load the changed data |
| 374 | | $data[$this->db->quote_identifier($key)] = $this->db->quote($this->data[$key]); |
| 375 | | } |
| 376 | | |
| 377 | | if ($this->auto_increment === TRUE) |
| 378 | | { |
| 379 | | // Remove the primary key from the insert |
| 380 | | unset($data[$this->primary_key]); |
| 381 | | } |
| 382 | | |
| 383 | | // Create the SQL statement |
| 384 | | $sql = 'INSERT INTO '.$this->db->quote_identifier($this->table).' ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $data).')'; |
| 385 | | } |
| 386 | | |
| 387 | | if ($count = $this->db->exec($sql)) |
| 388 | | { |
| 389 | | if ($insert === TRUE AND $this->auto_increment === TRUE) |
| 390 | | { |
| 391 | | // Get and assign the insert ID |
| 392 | | $this->data[$this->primary_key] = $this->db->lastInsertId(); |
| 393 | | } |
| 394 | | |
| 395 | | // No data has been changed |
| 396 | | $this->changed = array(); |
| 397 | | |
| | 489 | /** |
| | 490 | * Return the cached result of a query. If the result does not exist, it |
| | 491 | * will be created. The result will always be an array of objects. |
| | 492 | * |
| | 493 | * @param string cache key name |
| | 494 | * @param string SQL statement |
| | 495 | * @return array |
| | 496 | */ |
| | 497 | protected function cached_result($key, $sql) |
| | 498 | { |
| | 499 | // Cache key |
| | 500 | $key .= '-'.sha1($sql); |
| | 501 | |
| | 502 | // Retrieve the cached result |
| | 503 | $result = Cache::instance()->get($key); |
| | 504 | |
| | 505 | if ( ! is_array($result)) |
| | 506 | { |
| | 507 | // Prepare the query |
| | 508 | $query = $this->db->prepare($sql); |
| | 509 | |
| | 510 | // Execute the query |
| | 511 | $query->execute(); |
| | 512 | |
| | 513 | // Return all the rows |
| | 514 | $result = $query->fetchAll(PDO::FETCH_OBJ); |
| | 515 | |
| | 516 | // Set the cache |
| | 517 | Cache::instance()->set($key, $result, array('database', 'result'), strtotime('now +1 hour')); |
| | 518 | } |
| | 519 | |
| | 520 | // Return the cached result |
| | 521 | return $result; |
| | 522 | } |
| | 523 | |