Welcome to the Cookbook

loading...

3.8.3 Creating behavior methods

Behavior methods are automatically available on any model acting as the behavior. For example if you had:

class Duck extends AppModel {
	var $name = 'Duck';
	var $actsAs = array('Flying');
}
  1. class Duck extends AppModel {
  2. var $name = 'Duck';
  3. var $actsAs = array('Flying');
  4. }

You would be able to call FlyingBehavior methods as if they were methods on your Duck model. When creating behavior methods you automatically get passed a reference of the calling model as the first parameter. All other supplied parameters are shifted one place to the right. For example

$this->Category->fly('toronto', 'montreal');
  1. $this->Category->fly('toronto', 'montreal');
Although this method takes two parameters, the method signature should look like:
function fly(&$Model, $from, $to) {
	// Do some flying.
}
  1. function fly(&$Model, $from, $to) {
  2. // Do some flying.
  3. }
Keep in mind that methods called in a $this->doIt() fashion from inside a behavior method will not get the $model parameter automatically appended.