timestampable.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. = Timestampable Behavior =
  2. The `timestampable` behavior allows you to keep track of the date of creation and last update of your model objects.
  3. == Basic Usage ==
  4. In the `schema.xml`, use the `<behavior>` tag to add the `timestampable` behavior to a table:
  5. {{{
  6. #!xml
  7. <table name="book">
  8. <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
  9. <column name="title" type="VARCHAR" required="true" primaryString="true" />
  10. <behavior name="timestampable" />
  11. </table>
  12. }}}
  13. Rebuild your model, insert the table creation sql again, and you're ready to go. The model now has two new columns, `created_at` and `updated_at`, that store a timestamp automatically updated on save:
  14. {{{
  15. #!php
  16. <?php
  17. $b = new Book();
  18. $b->setTitle('War And Peace');
  19. $b->save();
  20. echo $b->getCreatedAt(); // 2009-10-02 18:14:23
  21. echo $b->getUpdatedAt(); // 2009-10-02 18:14:23
  22. $b->setTitle('Anna Karenina');
  23. $b->save();
  24. echo $b->getCreatedAt(); // 2009-10-02 18:14:23
  25. echo $b->getUpdatedAt(); // 2009-10-02 18:14:25
  26. }}}
  27. The object query also has specific methods to retrieve recent objects and order them according to their update date:
  28. {{{
  29. #!php
  30. <?php
  31. $books = BookQuery::create()
  32. ->recentlyUpdated() // adds a minimum value for the update date
  33. ->lastUpdatedFirst() // orders the results by descending update date
  34. ->find();
  35. }}}
  36. You can use any of the following methods in the object query:
  37. {{{
  38. #!php
  39. <?php
  40. // limits the query to recent objects
  41. ModelCriteria recentlyCreated($nbDays = 7)
  42. ModelCriteria recentlyUpdated($nbDays = 7)
  43. // orders the results
  44. ModelCriteria lastCreatedFirst() // order by creation date desc
  45. ModelCriteria firstCreatedFirst() // order by creation date asc
  46. ModelCriteria lastUpdatedFirst() // order by update date desc
  47. ModelCriteria firstUpdatedFirst() // order by update date asc
  48. }}}
  49. '''Tip''': You may need to keep the update date unchanged after an update on the object, for instance when you only update a calculated row. In this case, call the `keepUpdateDateUnchanged()` method on the object before saving it.
  50. == Parameters ==
  51. You can change the name of the columns added by the behavior by setting the `create_column` and `update_column` parameters:
  52. {{{
  53. #!xml
  54. <table name="book">
  55. <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
  56. <column name="title" type="VARCHAR" required="true" primaryString="true" />
  57. <column name="my_create_date" type="TIMESTAMP" />
  58. <column name="my_update_date" type="TIMESTAMP" />
  59. <behavior name="timestampable">
  60. <parameter name="create_column" value="my_create_date" />
  61. <parameter name="update_column" value="my_update_date" />
  62. </behavior>
  63. </table>
  64. }}}
  65. {{{
  66. #!php
  67. <?php
  68. $b = new Book();
  69. $b->setTitle('War And Peace');
  70. $b->save();
  71. echo $b->getMyCreateDate(); // 2009-10-02 18:14:23
  72. echo $b->getMyUpdateDate(); // 2009-10-02 18:14:23
  73. $b->setTitle('Anna Karenina');
  74. $b->save();
  75. echo $b->getMyCreateDate(); // 2009-10-02 18:14:23
  76. echo $b->getMyUpdateDate(); // 2009-10-02 18:14:25
  77. }}}