Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp - what is the difference between model and behavior?

I understand that the behavior is supposed to extend the model and add functionality to it, but in most cases the fat-model idea is making the behavior useless, isn't it?

And, even preferred, ignore my first paragraph and just answer - please - the question in the title, and add an example to make it clear

thanks

like image 527
yossi Avatar asked Aug 30 '11 20:08

yossi


People also ask

What is behaviour in CakePHP?

Behaviors are a way to organize and enable horizontal re-use of Model layer logic. Conceptually they are similar to traits. However, behaviors are implemented as separate classes. This allows them to hook into the life-cycle callbacks that models emit, while providing trait-like features.

What is CakePHP model?

Usually, model classes represent data and are used in CakePHP applications for data access. They generally represent a database table but can be used to access anything that manipulates data such as files, external web services, or iCal events. A model can be associated with other models.


2 Answers

A behavior is where you extract code that doesn't really belong in one specific models domain. Kind of like, helper functions, or a mixin/module (if you're familiar with that pattern from other programming languages).

If you're familiar with CakePHP helpers and components, you can look at it like this. A behavior is to Model as Helper is to View as Component is to Controller. Basically a set of functionality that will be used across multiple models.

Let's say you want to implement soft delete on all the models in your application. (Soft delete meaning, you don't actually delete the record, you just mark it as deleted). You wouldn't want to put the same soft delete code into every model. That's not very DRY! Instead you use a behavior to abstract out the functionality like so.

What we're trying to do is instead of delete the record, update the deleted on column with the current date (it will work the same way as created, modified). Then we'll change the find method to only retrieve records that aren't deleted.

// models/behaviors/soft_deletable.php
class SoftDeletableBehavior extends ModelBehavior {
    function setup(&$Model, $settings = array()) {
         // do any setup here
    }

    // override the delete function (behavior methods that override model methods take precedence)
    function delete(&$Model, $id = null) {
        $Model->id = $id;

        // save the deleted field with current date-time
        if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) {
            return true;
        }

        return false;
    }

    function beforeFind(&$Model, $query) {
         // only include records that have null deleted columns
         $query['conditions']["{$Model->alias}.deleted <>"] = '';
         return $query;
    }
}

Then in your model

Class User extends AppModel {
    public $actsAs = array('SoftDeletable');
}

And from your controller, you can call all of our behavior methods on your model

Class UsersControllers extends AppController {
    function someFunction() {
        $this->User->delete(1); // soft deletes user with id of 1

        $this->User->find('all'); // this will not exclude user with an id of 1
    }
}

I hope this helps you.

like image 75
Brandon Cordell Avatar answered Sep 28 '22 11:09

Brandon Cordell


Behaviors can be shared between Models. Typically a Behavior contains abstract code that can be applied to any model.

While you could of course write this for a single Model specifically, you would have to write it again for another Model. By abstracting it to be shared, you've created a Behavior.

In CakePHP a Behavior to a Model is the same relationship as a Component to a Controller or a Helper to a View.

An example of a Core Behavior in CakePHP is Containable. This allows you to have finer control over the relationships used in by find().

like image 35
Jason McCreary Avatar answered Sep 28 '22 10:09

Jason McCreary