Yii Framework Forum: Automated Model's Date Update Behaviors - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Automated Model's Date Update Behaviors Rate Topic: -----

#1 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 22 April 2013 - 07:09 AM

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)
{
	++$model->hits;
	$model->save();

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

For modification_date question is quite similar. What is the reason for using behavior? Why can't I use MySQL's build-in functionality of declaring "on_update CURRENT TIMESTAMP" attribute for column? Isn't it faster doing this in DB?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 22 April 2013 - 11:00 AM

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. :)
2

#3 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 22 April 2013 - 02:07 PM

Thanks for your ideas...

View Postbettor, on 22 April 2013 - 11:00 AM, said:

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.

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?

View Postbettor, on 22 April 2013 - 11:00 AM, said:

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.

As for updating creation / update date of model, using either MySQL functionality / or CTimestampBehavior is better than events (beforeSave()). As for, updating hit count. beforeSave() can't be used at all, as hit counter should updated each time model is loaded (viewed) not, each time it is saved.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 22 April 2013 - 02:29 PM

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.
"Less noise - more signal"
1

#5 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 22 April 2013 - 11:45 PM

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.
2

#6 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 23 April 2013 - 01:57 AM

View Postjacmoe, on 22 April 2013 - 02:29 PM, said:

That behavior does indeed use the beforeSave function. Think of it as reusable code.

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"?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#7 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 23 April 2013 - 01:58 AM

View Postwaterloomatt, on 22 April 2013 - 11:45 PM, said:

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.

Well... that's another good example and argument for using behaviors. Thanks! :]
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#8 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 23 April 2013 - 02:39 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users