ModelBehavior.php 8.0 KB

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