Cakephp - what is the difference between model and behavior?

I understand that behavior should extend the model and add functionality to it, but in most cases the idea of ​​a thick model makes the behavior useless, right?

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

thanks

+6
source share
3 answers

Behavior is where you retrieve code that really does not belong to the same model domain. For example, helper functions or mixin / module (if you are familiar with this template from other programming languages).

If you are familiar with CakePHP helpers and components, you can look at it this way. The behavior is in the model, since the assistant should be considered as a component for the controller. Basically a set of functionality that will be used for several models.

Suppose you want to implement soft deletion on all models of your application. (The soft value is delete, you are not actually deleting the record, you just mark it as deleted). You would not want to enter the same soft delete code in each model. This is not very DRY! Instead, you use behavior to abstract away from such functions.

What we are trying to do is not delete the record, update the deleted column with the current date (it will work the same as the created, modified). Then we modify the find method to retrieve only those records that are not 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('Ymd 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 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 } } 

Hope this helps you.

+22
source

Behavior can be shared between models. Typically, the behavior contains abstract code that can be applied to any model.

Although you could of course write this for a separate model, you will have to write it again for another model. By diverting it to sharing, you have created Behavior.

In CakePHP, the behavior for the model is the same as the component for the controller or view helper.

An example of the basic behavior in CakePHP is Containable . This allows you to more finely control the relationships used in find() .

+3
source

basically behavior is used to make your application dry! and code reuse ...

Check out this link ... it gives you a simple tag behavior that you can use in your publishing model

+1
source

Source: https://habr.com/ru/post/896247/


All Articles