Automated Model's Date Update Behaviors

I need to updated my model’s two columns (hit_count and modification_date). I know, that the best solution to achieve this, is to use model’s behaviors, but I’m fairly new to them, so I have some small doubts.

Base question for hit_count is, why should I use behaviors at all? Isn’t below form quicker than writing own behavior:

$model = $this->loadModel($id);

if($model != null)




	$this->render('view', array('model'=>$model));


[size=“2”]For modification_date question is quite similar. What is the reason for using behavior? Why can’t I use MySQL’s [/size][size=“2”]build[/size][size=“2”]-in [/size][size=“2”]functionality[/size][size=“2”] of declaring “on_update CURRENT TIMESTAMP” attribute for column? Isn’t it faster doing this in DB?[/size]

Generally, behaviors allow the addition of methods to an object. I am not sure why you are mentioning behaviors here at all as you are working with attributes. Did you mean events? What you are trying to achieve is more suitable for a beforeSave() event but there is nothing wrong in achieving it your way. :)

Thanks for your ideas…

Because automatic update of model’s creation / update timestamp has ben solved in core Yii using behaviors --> CTimestampBehavior.

I know, that this particular issue can be solved many ways. Some of them I mentioned in my post. However, I assume, that core Yii is coded by one of the best PHP developers around. If they (core Yii dev team) decided, that this functionality should solved using behaviors, then I assume, that this is best practice / best available technique.

But I’m still wonder, if solving this on DB side (i.e. using mentioned MySQL functionality) isn’t better / faster. I think, that yes, so I wonder, if in this particular case, CTimestampBehavior wasn’t developed as a helper class, not best practice?


[size=2]As for updating creation / update date of model, using either MySQL functionality / or CTimestampBehavior is better than events ([/size]beforeSave()[size=2]). As for, updating hit count. [/size]beforeSave()[size=2] can’t be used at all, as hit counter should updated each time model is loaded (viewed) not, each time it is saved.[/size]

That behavior does indeed use the beforeSave function. :)

Think of it as reusable code.

That’s all it is.

If you are using the same code for a lot of models, then use a model behavior.

Otherwise: don’t.

In my opinion, this feature needs to sit closer to the controller since it’s very hard for a model to distinguish between a find and view. A behavior works well but should explicitly be invoked through a controller’s events.

The DB approach could lead to a jam if you want to add complexity Ex. only visitor’s views (i.e. not admins) should be counted.

That explains something that is (should be) obvious to me: Behaviors are reusable, so I can use one to many models, while using beforeSave() or any similar is not reusable, so I have to duplicate code for each model (or use a common function, which is nothing else than just another form of behavior-reusable code).

But, your answer doesn’t explain, if (and why – if yes) using CTimestampBehavior is better than using MySQL feature of “ON UPDATE current_timestamp”?

Using behavior here is certainly better, if you plan a database migration or want to develop flexible application, that will be open for virtually any RDBMS. As other database system, other than MySQL, may not have this feature or may it have implemented different way. In such situation, after database migration you would have to manually update / change this part of code, while using behavior would save you from doing that.

Is that the only reason (did I answer myself? :]) or is there something else behind using CTimestampBehavior instead of MySQL feature of "ON UPDATE current_timestamp"?

Well… that’s another good example and argument for using behaviors. Thanks! :]

@Trejder In general, very good duscussion. Thanks for bringing it up :rolleyes: