CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclNode
  • Aco
  • AcoAction
  • Aro
  • BehaviorCollection
  • CakeSchema
  • ConnectionManager
  • I18nModel
  • Model
  • ModelBehavior
  • ModelValidator
  • Permission
  1: <?php
  2: /**
  3:  * BehaviorCollection
  4:  *
  5:  * Provides management and interface for interacting with collections of behaviors.
  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: App::uses('ObjectCollection', 'Utility');
 23: App::uses('CakeEventListener', 'Event');
 24: 
 25: /**
 26:  * Model behavior collection class.
 27:  *
 28:  * Defines the Behavior interface, and contains common model interaction functionality.
 29:  *
 30:  * @package       Cake.Model
 31:  */
 32: class BehaviorCollection extends ObjectCollection implements CakeEventListener {
 33: 
 34: /**
 35:  * Stores a reference to the attached name
 36:  *
 37:  * @var string
 38:  */
 39:     public $modelName = null;
 40: 
 41: /**
 42:  * Keeps a list of all methods of attached behaviors
 43:  *
 44:  * @var array
 45:  */
 46:     protected $_methods = array();
 47: 
 48: /**
 49:  * Keeps a list of all methods which have been mapped with regular expressions
 50:  *
 51:  * @var array
 52:  */
 53:     protected $_mappedMethods = array();
 54: 
 55: /**
 56:  * Attaches a model object and loads a list of behaviors
 57:  *
 58:  * @param string $modelName
 59:  * @param array $behaviors
 60:  * @return void
 61:  */
 62:     public function init($modelName, $behaviors = array()) {
 63:         $this->modelName = $modelName;
 64: 
 65:         if (!empty($behaviors)) {
 66:             foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $behavior => $config) {
 67:                 $this->load($config['class'], $config['settings']);
 68:             }
 69:         }
 70:     }
 71: 
 72: /**
 73:  * Backwards compatible alias for load()
 74:  *
 75:  * @param string $behavior
 76:  * @param array $config
 77:  * @return void
 78:  * @deprecated Replaced with load()
 79:  */
 80:     public function attach($behavior, $config = array()) {
 81:         return $this->load($behavior, $config);
 82:     }
 83: 
 84: /**
 85:  * Loads a behavior into the collection. You can use use `$config['enabled'] = false`
 86:  * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
 87:  * can still be used as normal.
 88:  *
 89:  * You can alias your behavior as an existing behavior by setting the 'className' key, i.e.,
 90:  * {{{
 91:  * public $actsAs = array(
 92:  *   'Tree' => array(
 93:  *     'className' => 'AliasedTree'
 94:  *   );
 95:  * );
 96:  * }}}
 97:  * All calls to the `Tree` behavior would use `AliasedTree` instead.
 98:  *
 99:  * @param string $behavior CamelCased name of the behavior to load
100:  * @param array $config Behavior configuration parameters
101:  * @return boolean True on success, false on failure
102:  * @throws MissingBehaviorException when a behavior could not be found.
103:  */
104:     public function load($behavior, $config = array()) {
105:         if (is_array($config) && isset($config['className'])) {
106:             $alias = $behavior;
107:             $behavior = $config['className'];
108:         }
109:         $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
110:         unset($config['enabled'], $config['className']);
111: 
112:         list($plugin, $name) = pluginSplit($behavior, true);
113:         if (!isset($alias)) {
114:             $alias = $name;
115:         }
116: 
117:         $class = $name . 'Behavior';
118: 
119:         App::uses($class, $plugin . 'Model/Behavior');
120:         if (!class_exists($class)) {
121:             throw new MissingBehaviorException(array(
122:                 'class' => $class,
123:                 'plugin' => substr($plugin, 0, -1)
124:             ));
125:         }
126: 
127:         if (!isset($this->{$alias})) {
128:             if (ClassRegistry::isKeySet($class)) {
129:                 $this->_loaded[$alias] = ClassRegistry::getObject($class);
130:             } else {
131:                 $this->_loaded[$alias] = new $class();
132:                 ClassRegistry::addObject($class, $this->_loaded[$alias]);
133:                 if (!empty($plugin)) {
134:                     ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
135:                 }
136:             }
137:         } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
138:             if ($config !== null && $config !== false) {
139:                 $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
140:             } else {
141:                 $config = array();
142:             }
143:         }
144:         if (empty($config)) {
145:             $config = array();
146:         }
147:         $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
148: 
149:         foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
150:             $this->_mappedMethods[$method] = array($alias, $methodAlias);
151:         }
152:         $methods = get_class_methods($this->_loaded[$alias]);
153:         $parentMethods = array_flip(get_class_methods('ModelBehavior'));
154:         $callbacks = array(
155:             'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
156:             'beforeDelete', 'afterDelete', 'onError'
157:         );
158: 
159:         foreach ($methods as $m) {
160:             if (!isset($parentMethods[$m])) {
161:                 $methodAllowed = (
162:                     $m[0] != '_' && !array_key_exists($m, $this->_methods) &&
163:                     !in_array($m, $callbacks)
164:                 );
165:                 if ($methodAllowed) {
166:                     $this->_methods[$m] = array($alias, $m);
167:                 }
168:             }
169:         }
170: 
171:         if (!in_array($alias, $this->_enabled) && !$configDisabled) {
172:             $this->enable($alias);
173:         } else {
174:             $this->disable($alias);
175:         }
176:         return true;
177:     }
178: 
179: /**
180:  * Detaches a behavior from a model
181:  *
182:  * @param string $name CamelCased name of the behavior to unload
183:  * @return void
184:  */
185:     public function unload($name) {
186:         list($plugin, $name) = pluginSplit($name);
187:         if (isset($this->_loaded[$name])) {
188:             $this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
189:             parent::unload($name);
190:         }
191:         foreach ($this->_methods as $m => $callback) {
192:             if (is_array($callback) && $callback[0] == $name) {
193:                 unset($this->_methods[$m]);
194:             }
195:         }
196:     }
197: 
198: /**
199:  * Backwards compatible alias for unload()
200:  *
201:  * @param string $name Name of behavior
202:  * @return void
203:  * @deprecated Use unload instead.
204:  */
205:     public function detach($name) {
206:         return $this->unload($name);
207:     }
208: 
209: /**
210:  * Dispatches a behavior method.  Will call either normal methods or mapped methods.
211:  *
212:  * If a method is not handled by the BehaviorCollection, and $strict is false, a
213:  * special return of `array('unhandled')` will be returned to signal the method was not found.
214:  *
215:  * @param Model $model The model the method was originally called on.
216:  * @param string $method The method called.
217:  * @param array $params Parameters for the called method.
218:  * @param boolean $strict If methods are not found, trigger an error.
219:  * @return array All methods for all behaviors attached to this object
220:  */
221:     public function dispatchMethod($model, $method, $params = array(), $strict = false) {
222:         $method = $this->hasMethod($method, true);
223: 
224:         if ($strict && empty($method)) {
225:             trigger_error(__d('cake_dev', "BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior", $method), E_USER_WARNING);
226:             return null;
227:         }
228:         if (empty($method)) {
229:             return array('unhandled');
230:         }
231:         if (count($method) === 3) {
232:             array_unshift($params, $method[2]);
233:             unset($method[2]);
234:         }
235:         return call_user_func_array(
236:             array($this->_loaded[$method[0]], $method[1]),
237:             array_merge(array(&$model), $params)
238:         );
239:     }
240: 
241: /**
242:  * Gets the method list for attached behaviors, i.e. all public, non-callback methods.
243:  * This does not include mappedMethods.
244:  *
245:  * @return array All public methods for all behaviors attached to this collection
246:  */
247:     public function methods() {
248:         return $this->_methods;
249:     }
250: 
251: /**
252:  * Check to see if a behavior in this collection implements the provided method.  Will
253:  * also check mappedMethods.
254:  *
255:  * @param string $method The method to find.
256:  * @param boolean $callback Return the callback for the method.
257:  * @return mixed If $callback is false, a boolean will be returned, if its true, an array
258:  *   containing callback information will be returned.  For mapped methods the array will have 3 elements.
259:  */
260:     public function hasMethod($method, $callback = false) {
261:         if (isset($this->_methods[$method])) {
262:             return $callback ? $this->_methods[$method] : true;
263:         }
264:         foreach ($this->_mappedMethods as $pattern => $target) {
265:             if (preg_match($pattern . 'i', $method)) {
266:                 if ($callback) {
267:                     $target[] = $method;
268:                     return $target;
269:                 }
270:                 return true;
271:             }
272:         }
273:         return false;
274:     }
275: 
276: /**
277:  * Returns the implemented events that will get routed to the trigger function
278:  * in order to dispatch them separately on each behavior
279:  *
280:  * @return array
281:  */
282:     public function implementedEvents() {
283:         return array(
284:             'Model.beforeFind' => 'trigger',
285:             'Model.afterFind' => 'trigger',
286:             'Model.beforeValidate' => 'trigger',
287:             'Model.afterValidate' => 'trigger',
288:             'Model.beforeSave' => 'trigger',
289:             'Model.afterSave' => 'trigger',
290:             'Model.beforeDelete' => 'trigger',
291:             'Model.afterDelete' => 'trigger'
292:         );
293:     }
294: 
295: }
296: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs