warehouse.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php namespace System\DB\Eloquent;
  2. class Warehouse {
  3. /**
  4. * Save an Eloquent model to the database.
  5. *
  6. * @param object $eloquent
  7. * @return bool
  8. */
  9. public static function store($eloquent)
  10. {
  11. $model = get_class($eloquent);
  12. // -----------------------------------------------------
  13. // Get a fresh query instance for the model.
  14. // -----------------------------------------------------
  15. $eloquent->query = \System\DB\Query::table(Meta::table($model));
  16. // -----------------------------------------------------
  17. // Set the creation and update timestamps.
  18. // -----------------------------------------------------
  19. if (property_exists($model, 'timestamps') and $model::$timestamps)
  20. {
  21. static::timestamp($eloquent);
  22. }
  23. if ($eloquent->exists)
  24. {
  25. return ($eloquent->query->where('id', '=', $eloquent->attributes['id'])->update($eloquent->dirty) == 1) ? true : false;
  26. }
  27. else
  28. {
  29. $eloquent->attributes['id'] = $eloquent->query->insert_get_id($eloquent->attributes);
  30. }
  31. $eloquent->exists = true;
  32. return true;
  33. }
  34. /**
  35. * Set the activity timestamps on a model.
  36. *
  37. * @param object $eloquent
  38. * @return void
  39. */
  40. private static function timestamp($eloquent)
  41. {
  42. $eloquent->updated_at = date('Y-m-d H:i:s');
  43. if ( ! $eloquent->exists)
  44. {
  45. $eloquent->created_at = $eloquent->updated_at;
  46. }
  47. }
  48. }