Monday, August 1, 2011

Keeping your Yii Models Lean - Use Behaviors

Active Record models are fantastic for consolidating "black box" logic and keeping your models self-aware of their business logic, but what do you do when the business rules and object specific operations keep adding up?

Do yourself a favor, and get into the habit of putting those business logic methods into attachable behaviors.


If you're not familiar with Behaviors at all, you can catch the basics here:
http://www.yiiframework.com/doc/api/1.1/CBehavior
http://www.yiiframework.com/doc/api/1.1/CActiveRecordBehavior

Not only does it make it easier to maintain your code, but it makes it much easier for a team to collaborate within the same area of development. You'll also end up with a nice library of components which you can reuse.

For example, there is the built in timestamp behavior for updating timestamp fields on CActiveRecord models. See: http://www.yiiframework.com/doc/api/1.1/CTimestampBehavior/


Need to ensure that some special logging happens each time a model is saved? Create a CActiveRecordBehavior for the model that has an afterSave method defined to run your checks and fire off the required logs/emails/what have you. You can use the same behavior for multiple models, so that if the logic needs to change, you can easily do so by changing just the behavior and not touch your core models.

Even if the logical process only applies to the one model, it's worth putting it into a behavior just so that you can adjust the business end of it without modifying the core model.

For example, if you have a 'Blog' model that requires approval before it can be publicly visible, it makes sense to put the 'approve' method, right on the model so that you can access it consistently from multiple controller actions/etc. But even better, is to make that approve method a behavior that is attached to the model, along with the 'reject', etc. methods. Then, if you need to regenerate your core model for some reason (yii upgrade perhaps??) you can do so without losing your business logic pieces and conversely, if you need to update the approval process in some way, you can do so without the potential of breaking your blog model or have one person developing the approval process while another person works on the validation routines, etc. without conflict.

This is especially important when you have very complex rules that need to be applied to a model consistently regardless of where it is accessed from, so it's a good habit to get into early.

One of the rules of thumb for this is, if you find yourself adding extra properties to the model to track information related to some business process, it's a good bet you should be making that a behavior.

9 comments:

  1. Very good catch, Dana. I've been using Yii for a couple of months and never got the real usage of CBehavior. With your explanation, you enlightened me. Thanks

    ReplyDelete
  2. Highly understandable and useful. The thing I hate most about Yii is that I can read about it and wonder why the writing makes less sense than actual code. That should never happen. Your writing makes sense. Thanks for sharing.

    ReplyDelete
  3. Thanks, I'm glad you found it helpful!

    Sometimes, I think when you're coding something and you really understand it from the inside out, it's very hard to write about it in a way that the people who didn't create it can easily understand, because you take a lot for granted as 'understood'.

    ReplyDelete
  4. Hi Dana

    I read your post and its really good, i had a chance of working with logging system but i implemented it using events handling way, whenever a record is created an event get fired and it makes logging

    How do you comment about having events for logging purpose or you would prefer behaviours ?

    ReplyDelete
  5. Great post. I would like to also mention that by writing tests on your models, it can make it easier to refactor without worrying about if you broke something. For our open source crm application, Zurmo (zurmo.org) we are going to utilize events/behaviors to handle some workflow hooks that will allow customers to customize the models logic without having to modify the code.

    ReplyDelete
  6. Could you pls give me an example on how to implement behavior..

    ReplyDelete
  7. Priya,

    I hope the new blog post answers your question!

    - Dana

    ReplyDelete
  8. I dont think that activerecord is the place the business logic, when you have complicated business logic (eg: enterprise app) it breaks the single responsabilty priciple and activerecord will be an anti-pattern, I suggest using components to separate the business layer from the database access layer

    ReplyDelete