FAQ #47
Question: What is a behavior?
Answer: It's what a component is to controllers, but for models, simply a way to share functions among models. It also has several callback functions which you can implement in order to intercept model actions. To make a model use your behaviors, you have to specify via the $actAs property in the model's class. All your model behaviors should be stored in app/models/behaviors or the corresponding directory if it's in a plugin. Cake includes a pointer to the model calling your behavior as the first argument passed to your method. Exception made of the callback functions, you can call your behavior's method directly through your model.
//in your model you use $actAs to include behaviors
class Cook extends appModel{
var $actAs=array('MyBad');
//in my controller
class SpoonsController extends AppController(){
var $uses=array('Cook');
function whatever(){
//I can call the behavior's methods through the model object
$this->Cook->myFunction('jediknight');
}
There are too many things regarding behavior callbacks to discuss them all here! Visit the corresponding Cakephp cookbook and api urls provided below.
Example:
//my behavior class
class MyBadBehavior extends ModelBehavior{
//Setup is called by Cake first. You can do preliminary stuff here, including
save the settings passed via the $actAs array in the model
function setup(&$model,$settings=array()){
//let's save settings
$this->settings=$settings;
}
//callback method invoked before the data is saved, affording us an opportunity to modify said data, or add further data
function beforeSave(&$model){
//in our example, if the model includes a field for ip addresses, we set it here. the model object's data array contains the data to be saved, while the _schema array describes the table (usually a table, that is!) we'll be saving to.
$modelName=$model->name;
if(!isset($model->data[$modelName]['ip'])&&isset($model->_schema['ip'])){
$model->data[$modelName]['ip']=env('REMOTE_ADDR');
}
}
//here's a custom behavior method, as opposed to a callback. It's called directly from a controller that uses the model using the behavior (!)
function myFunction(&$model,$secret_code){
$modelName=$model->name;
$model->data[$modelName]['secret_code']=$secret_code;
}
...
class MyBadBehavior extends ModelBehavior{
//Setup is called by Cake first. You can do preliminary stuff here, including
save the settings passed via the $actAs array in the model
function setup(&$model,$settings=array()){
//let's save settings
$this->settings=$settings;
}
//callback method invoked before the data is saved, affording us an opportunity to modify said data, or add further data
function beforeSave(&$model){
//in our example, if the model includes a field for ip addresses, we set it here. the model object's data array contains the data to be saved, while the _schema array describes the table (usually a table, that is!) we'll be saving to.
$modelName=$model->name;
if(!isset($model->data[$modelName]['ip'])&&isset($model->_schema['ip'])){
$model->data[$modelName]['ip']=env('REMOTE_ADDR');
}
}
//here's a custom behavior method, as opposed to a callback. It's called directly from a controller that uses the model using the behavior (!)
function myFunction(&$model,$secret_code){
$modelName=$model->name;
$model->data[$modelName]['secret_code']=$secret_code;
}
...
//in your model you use $actAs to include behaviors
class Cook extends appModel{
var $actAs=array('MyBad');
//in my controller
class SpoonsController extends AppController(){
var $uses=array('Cook');
function whatever(){
//I can call the behavior's methods through the model object
$this->Cook->myFunction('jediknight');
}
Warning:
The Cakephp cookbook doesn't describe the structure of the model object passed to behaviors. Best way for you to learn about it is to print out the object to screen and examine it!
Additional notes:
Category:Model