PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Model/ModelBehavior.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 220 lines | 40 code | 15 blank | 165 comment | 4 complexity | 71314859596f05533ba5e20b316a2076 MD5 | raw file
  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-2011, 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-2011, 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, $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, $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, $config = array()) { }
  92. /**
  93. * Clean up any initialization this behavior has done on a model. Called when a behavior is dynamically
  94. * detached from a model using Model::detach().
  95. *
  96. * @param Model $model Model using this behavior
  97. * @return void
  98. * @see BehaviorCollection::detach()
  99. */
  100. public function cleanup($model) {
  101. if (isset($this->settings[$model->alias])) {
  102. unset($this->settings[$model->alias]);
  103. }
  104. }
  105. /**
  106. * beforeFind can be used to cancel find operations, or modify the query that will be executed.
  107. * By returning null/false you can abort a find. By returning an array you can modify/replace the query
  108. * that is going to be run.
  109. *
  110. * @param Model $model Model using this behavior
  111. * @param array $query Data used to execute this query, i.e. conditions, order, etc.
  112. * @return boolean|array False or null will abort the operation. You can return an array to replace the
  113. * $query that will be eventually run.
  114. */
  115. public function beforeFind($model, $query) {
  116. return true;
  117. }
  118. /**
  119. * After find callback. Can be used to modify any results returned by find.
  120. *
  121. * @param Model $model Model using this behavior
  122. * @param mixed $results The results of the find operation
  123. * @param boolean $primary Whether this model is being queried directly (vs. being queried as an association)
  124. * @return mixed An array value will replace the value of $results - any other value will be ignored.
  125. */
  126. public function afterFind($model, $results, $primary) { }
  127. /**
  128. * beforeValidate is called before a model is validated, you can use this callback to
  129. * add behavior validation rules into a models validate array. Returning false
  130. * will allow you to make the validation fail.
  131. *
  132. * @param Model $model Model using this behavior
  133. * @return mixed False or null will abort the operation. Any other result will continue.
  134. */
  135. public function beforeValidate($model) {
  136. return true;
  137. }
  138. /**
  139. * beforeSave is called before a model is saved. Returning false from a beforeSave callback
  140. * will abort the save operation.
  141. *
  142. * @param Model $model Model using this behavior
  143. * @return mixed False if the operation should abort. Any other result will continue.
  144. */
  145. public function beforeSave($model) {
  146. return true;
  147. }
  148. /**
  149. * afterSave is called after a model is saved.
  150. *
  151. * @param Model $model Model using this behavior
  152. * @param boolean $created True if this save created a new record
  153. * @return boolean
  154. */
  155. public function afterSave($model, $created) {
  156. return true;
  157. }
  158. /**
  159. * Before delete is called before any delete occurs on the attached model, but after the model's
  160. * beforeDelete is called. Returning false from a beforeDelete will abort the delete.
  161. *
  162. * @param Model $model Model using this behavior
  163. * @param boolean $cascade If true records that depend on this record will also be deleted
  164. * @return mixed False if the operation should abort. Any other result will continue.
  165. */
  166. public function beforeDelete($model, $cascade = true) {
  167. return true;
  168. }
  169. /**
  170. * After delete is called after any delete occurs on the attached model.
  171. *
  172. * @param Model $model Model using this behavior
  173. * @return void
  174. */
  175. public function afterDelete($model) { }
  176. /**
  177. * DataSource error callback
  178. *
  179. * @param Model $model Model using this behavior
  180. * @param string $error Error generated in DataSource
  181. * @return void
  182. */
  183. public function onError($model, $error) { }
  184. /**
  185. * If $model's whitelist property is non-empty, $field will be added to it.
  186. * Note: this method should *only* be used in beforeValidate or beforeSave to ensure
  187. * that it only modifies the whitelist for the current save operation. Also make sure
  188. * you explicitly set the value of the field which you are allowing.
  189. *
  190. * @param Model $model Model using this behavior
  191. * @param string $field Field to be added to $model's whitelist
  192. * @return void
  193. */
  194. protected function _addToWhitelist($model, $field) {
  195. if (is_array($field)) {
  196. foreach ($field as $f) {
  197. $this->_addToWhitelist($model, $f);
  198. }
  199. return;
  200. }
  201. if (!empty($model->whitelist) && !in_array($field, $model->whitelist)) {
  202. $model->whitelist[] = $field;
  203. }
  204. }
  205. }