createdat.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Orm;
  13. /**
  14. * CreatedAt observer. Makes sure the created timestamp column in a Model record
  15. * gets a value when a new record is inserted in the database.
  16. */
  17. class Observer_CreatedAt extends Observer
  18. {
  19. /**
  20. * @var bool default setting, true to use mySQL timestamp instead of UNIX timestamp
  21. */
  22. public static $mysql_timestamp = false;
  23. /**
  24. * @var string default property to set the timestamp on
  25. */
  26. public static $property = 'created_at';
  27. /**
  28. * @var bool true to use mySQL timestamp instead of UNIX timestamp
  29. */
  30. protected $_mysql_timestamp;
  31. /**
  32. * @var string property to set the timestamp on
  33. */
  34. protected $_property;
  35. /**
  36. * Set the properties for this observer instance, based on the parent model's
  37. * configuration or the defined defaults.
  38. *
  39. * @param string Model class this observer is called on
  40. */
  41. public function __construct($class)
  42. {
  43. $props = $class::observers(get_class($this));
  44. $this->_mysql_timestamp = isset($props['mysql_timestamp']) ? $props['mysql_timestamp'] : static::$mysql_timestamp;
  45. $this->_property = isset($props['property']) ? $props['property'] : static::$property;
  46. }
  47. /**
  48. * Set the CreatedAt property to the current time.
  49. *
  50. * @param Model Model object subject of this observer method
  51. */
  52. public function before_insert(Model $obj)
  53. {
  54. $obj->{$this->_property} = $this->_mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
  55. }
  56. }