ModelBehavior.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Model behaviors base class.
  4. *
  5. * Adds methods and automagic functionality to Cake Models.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Model
  18. * @since CakePHP(tm) v 1.2.0.0
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Model behavior base class.
  23. *
  24. * Defines the Behavior interface, and contains common model interaction functionality. Behaviors
  25. * allow you to simulate mixins, and create reusable blocks of application logic, that can be reused across
  26. * several models. Behaviors also provide a way to hook into model callbacks and augment their behavior.
  27. *
  28. * ### Mixin methods
  29. *
  30. * Behaviors can provide mixin like features by declaring public methods. These methods should expect
  31. * the model instance to be shifted onto the parameter list.
  32. *
  33. * {{{
  34. * function doSomething(Model $model, $arg1, $arg2) {
  35. * //do something
  36. * }
  37. * }}}
  38. *
  39. * Would be called like `$this->Model->doSomething($arg1, $arg2);`.
  40. *
  41. * ### Mapped methods
  42. *
  43. * Behaviors can also define mapped methods. Mapped methods use pattern matching for method invocation. This
  44. * allows you to create methods similar to Model::findAllByXXX methods on your behaviors. Mapped methods need to
  45. * be declared in your behaviors `$mapMethods` array. The method signature for a mapped method is slightly different
  46. * than a normal behavior mixin method.
  47. *
  48. * {{{
  49. * public $mapMethods = array('/do(\w+)/' => 'doSomething');
  50. *
  51. * function doSomething(Model $model, $method, $arg1, $arg2) {
  52. * //do something
  53. * }
  54. * }}}
  55. *
  56. * The above will map every doXXX() method call to the behavior. As you can see, the model is
  57. * still the first parameter, but the called method name will be the 2nd parameter. This allows
  58. * you to munge the method name for additional information, much like Model::findAllByXX.
  59. *
  60. * @package Cake.Model
  61. * @see Model::$actsAs
  62. * @see BehaviorCollection::load()
  63. */
  64. class ModelBehavior extends Object {
  65. /**
  66. * Contains configuration settings for use with individual model objects. This
  67. * is used because if multiple models use this Behavior, each will use the same
  68. * object instance. Individual model settings should be stored as an
  69. * associative array, keyed off of the model name.
  70. *
  71. * @var array
  72. * @see Model::$alias
  73. */
  74. public $settings = array();
  75. /**
  76. * Allows the mapping of preg-compatible regular expressions to public or
  77. * private methods in this class, where the array key is a /-delimited regular
  78. * expression, and the value is a class method. Similar to the functionality of
  79. * the findBy* / findAllBy* magic methods.
  80. *
  81. * @var array
  82. */
  83. public $mapMethods = array();
  84. /**
  85. * Setup this behavior with the specified configuration settings.
  86. *
  87. * @param Model $model Model using this behavior
  88. * @param array $config Configuration settings for $model
  89. * @return void
  90. */
  91. public function setup(Model $model, $config = array()) {
  92. }
  93. /**
  94. * Clean up any initialization this behavior has done on a model. Called when a behavior is dynamically
  95. * detached from a model using Model::detach().
  96. *
  97. * @param Model $model Model using this behavior
  98. * @return void
  99. * @see BehaviorCollection::detach()
  100. */
  101. public function cleanup(Model $model) {
  102. if (isset($this->settings[$model->alias])) {
  103. unset($this->settings[$model->alias]);
  104. }
  105. }
  106. /**
  107. * beforeFind can be used to cancel find operations, or modify the query that will be executed.
  108. * By returning null/false you can abort a find. By returning an array you can modify/replace the query
  109. * that is going to be run.
  110. *
  111. * @param Model $model Model using this behavior
  112. * @param array $query Data used to execute this query, i.e. conditions, order, etc.
  113. * @return boolean|array False or null will abort the operation. You can return an array to replace the
  114. * $query that will be eventually run.
  115. */
  116. public function beforeFind(Model $model, $query) {
  117. return true;
  118. }
  119. /**
  120. * After find callback. Can be used to modify any results returned by find.
  121. *
  122. * @param Model $model Model using this behavior
  123. * @param mixed $results The results of the find operation
  124. * @param boolean $primary Whether this model is being queried directly (vs. being queried as an association)
  125. * @return mixed An array value will replace the value of $results - any other value will be ignored.
  126. */
  127. public function afterFind(Model $model, $results, $primary) {
  128. }
  129. /**
  130. * beforeValidate is called before a model is validated, you can use this callback to
  131. * add behavior validation rules into a models validate array. Returning false
  132. * will allow you to make the validation fail.
  133. *
  134. * @param Model $model Model using this behavior
  135. * @return mixed False or null will abort the operation. Any other result will continue.
  136. */
  137. public function beforeValidate(Model $model) {
  138. return true;
  139. }
  140. /**
  141. * afterValidate is called just after model data was validated, you can use this callback
  142. * to perform any data cleanup or preparation if needed
  143. *
  144. * @param Model $model Model using this behavior
  145. * @return mixed False will stop this event from being passed to other behaviors
  146. */
  147. public function afterValidate(Model $model) {
  148. return true;
  149. }
  150. /**
  151. * beforeSave is called before a model is saved. Returning false from a beforeSave callback
  152. * will abort the save operation.
  153. *
  154. * @param Model $model Model using this behavior
  155. * @return mixed False if the operation should abort. Any other result will continue.
  156. */
  157. public function beforeSave(Model $model) {
  158. return true;
  159. }
  160. /**
  161. * afterSave is called after a model is saved.
  162. *
  163. * @param Model $model Model using this behavior
  164. * @param boolean $created True if this save created a new record
  165. * @return boolean
  166. */
  167. public function afterSave(Model $model, $created) {
  168. return true;
  169. }
  170. /**
  171. * Before delete is called before any delete occurs on the attached model, but after the model's
  172. * beforeDelete is called. Returning false from a beforeDelete will abort the delete.
  173. *
  174. * @param Model $model Model using this behavior
  175. * @param boolean $cascade If true records that depend on this record will also be deleted
  176. * @return mixed False if the operation should abort. Any other result will continue.
  177. */
  178. public function beforeDelete(Model $model, $cascade = true) {
  179. return true;
  180. }
  181. /**
  182. * After delete is called after any delete occurs on the attached model.
  183. *
  184. * @param Model $model Model using this behavior
  185. * @return void
  186. */
  187. public function afterDelete(Model $model) {
  188. }
  189. /**
  190. * DataSource error callback
  191. *
  192. * @param Model $model Model using this behavior
  193. * @param string $error Error generated in DataSource
  194. * @return void
  195. */
  196. public function onError(Model $model, $error) {
  197. }
  198. /**
  199. * If $model's whitelist property is non-empty, $field will be added to it.
  200. * Note: this method should *only* be used in beforeValidate or beforeSave to ensure
  201. * that it only modifies the whitelist for the current save operation. Also make sure
  202. * you explicitly set the value of the field which you are allowing.
  203. *
  204. * @param Model $model Model using this behavior
  205. * @param string $field Field to be added to $model's whitelist
  206. * @return void
  207. */
  208. protected function _addToWhitelist(Model $model, $field) {
  209. if (is_array($field)) {
  210. foreach ($field as $f) {
  211. $this->_addToWhitelist($model, $f);
  212. }
  213. return;
  214. }
  215. if (!empty($model->whitelist) && !in_array($field, $model->whitelist)) {
  216. $model->whitelist[] = $field;
  217. }
  218. }
  219. }