ResetBehavior.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. /**
  4. * Allows the model to reset all records as batch command.
  5. * This way any slugging, geocoding or other beforeValidate, beforeSave, ... callbacks
  6. * can be retriggered for them.
  7. *
  8. * By default it will not update the modified timestamp and will re-save id and displayName.
  9. * If you need more fields, you need to specify them manually.
  10. *
  11. * You can also disable validate callback or provide a conditions scope to match only a subset
  12. * of records.
  13. *
  14. * For performance and memory reasons the records will only be processed in loops (not all at once).
  15. * If you have time-sensitive data, you can modify the limit of records per loop as well as the
  16. * timeout in between each loop.
  17. * Remember to raise set_time_limit() if you do not run this via CLI.
  18. *
  19. * It is recommended to attach this behavior dynamically where needed:
  20. *
  21. * $this->Model->Behaviors->load('Tools.Reset', array(...));
  22. * $this->Model->resetRecords();
  23. *
  24. * For tables with lots of records you might want to use a shell and the CLI to invoke the reset/update process.
  25. *
  26. * @author Mark Scherer
  27. * @license MIT
  28. * @cakephp 2.x
  29. * @version 1.0
  30. */
  31. class ResetBehavior extends ModelBehavior {
  32. protected $_defaults = array(
  33. 'limit' => 100, // batch of records per loop
  34. 'timeout' => null, // in seconds
  35. 'fields' => array(), // if not displayField
  36. 'validate' => true, // trigger beforeValidate callback
  37. 'updateTimestamp' => false, // update modified/updated timestamp
  38. 'scope' => array(), // optional conditions
  39. 'callback' => null,
  40. );
  41. /**
  42. * Configure the behavior through the Model::actsAs property
  43. *
  44. * @param object $Model
  45. * @param array $config
  46. */
  47. public function setup(Model $Model, $config = null) {
  48. if (is_array($config)) {
  49. $this->settings[$Model->alias] = array_merge($this->_defaults, $config);
  50. } else {
  51. $this->settings[$Model->alias] = $this->_defaults;
  52. }
  53. }
  54. /**
  55. * Regenerate all records (including possible beforeValidate/beforeSave callbacks).
  56. *
  57. * @param Model $Model
  58. * @param array $conditions
  59. * @param integer $recursive
  60. * @return boolean Success
  61. */
  62. public function resetRecords(Model $Model, $params = array()) {
  63. $recursive = -1;
  64. extract($this->settings[$Model->alias]);
  65. $defaults = array(
  66. 'page' => 1,
  67. 'limit' => $limit,
  68. 'fields' => array(),
  69. 'order' => $Model->alias.'.'.$Model->displayField . ' ASC',
  70. 'conditions' => $scope,
  71. 'recursive' => $recursive,
  72. );
  73. if (!empty($fields)) {
  74. if (!$Model->hasField($fields)) {
  75. throw new CakeException('Model does not have fields ' . print_r($fields, true));
  76. }
  77. $defaults['fields'] = array_merge(array($Model->primaryKey), $fields);
  78. } else {
  79. $defaults['fields'] = array($Model->primaryKey);
  80. if ($Model->displayField !== $Model->primaryKey) {
  81. $defaults['fields'][] = $Model->displayField;
  82. }
  83. }
  84. if (!$updateTimestamp) {
  85. $fields = array('modified', 'updated');
  86. foreach ($fields as $field) {
  87. if ($Model->schema($field)) {
  88. $defaults['fields'][] = $field;
  89. break;
  90. }
  91. }
  92. }
  93. $params = array_merge($defaults, $params);
  94. $count = $Model->find('count', compact('conditions'));
  95. $max = ini_get('max_execution_time');
  96. if ($max) {
  97. set_time_limit (max($max, $count / $limit));
  98. }
  99. while ($rows = $Model->find('all', $params)) {
  100. foreach ($rows as $row) {
  101. $Model->create();
  102. $fields = $params['fields'];
  103. if ($callback) {
  104. $Model->{$callback}($row, $fields);
  105. }
  106. $res = $Model->save($row, $validate, $fields);
  107. if (!$res) {
  108. throw new CakeException(print_r($Model->validationErrors, true));
  109. }
  110. }
  111. $params['page']++;
  112. if ($timeout) {
  113. sleep((int)$timeout);
  114. }
  115. }
  116. return true;
  117. }
  118. }