cakephp - what is the difference between model and behavior?
Asked Answered
F

3

7

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

Formulary answered 30/8, 2011 at 20:24 Comment(0)
R
26

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.

Revolt answered 2/9, 2011 at 6:3 Comment(3)
Great answer! very readable and understandable code example! thank you.Formulary
You're welcome Yossi. I was hoping it wasn't too long winded!Revolt
nope, it was perfect.. that is why.. can you please check this? #7278857Formulary
D
3

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().

Dichromatic answered 30/8, 2011 at 20:33 Comment(0)
A
1

basically behaviors are used to make your application dry! and code reuse...

Check this link... it gives you simple tagging behavior which you can use in your post model

Ailbert answered 30/8, 2011 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.