0

I have the database like this

=== Group ===
id
member_id
decsription

=== Member ===
id
name
description

Everything is working fine.Now I extended my Group table and added some extra field like created_by and updated_by,by which I can track who has created and who has made the changes.So to achieve what should I do and how to do that?Any help and suggestions will be highly appreciable.

tereško
  • 58,060
  • 25
  • 98
  • 150
NewUser
  • 12,713
  • 39
  • 142
  • 236

3 Answers3

1

You could extend beforeSave / beforeUpdate in your AR class and simply set $this->updated_by = Yii::app()->user->getId(); on update. If you override these methods, make sure to return true when you are done so that you do not prevent save. You can also set created_at and updated_at to new CDbExpression('NOW()'); so that these are always up to date as well.

Hope this helps!

Note: Not saying it's very nice to have your AR models know about the currently signed in user, but it should work just fine and it's worth considering.

Eirik Hoem
  • 1,310
  • 7
  • 14
  • 2
    I would strongly recommend using timestamps in the db instead of datetime, due to timezone issues. also, by putting Yii::app()->user->id in your models, you have to be careful about unit testing and command line usage. Make sure to set Yii::app()->user->id to something useful in those situations. – Neil McGuigan Feb 18 '12 at 22:10
0

If its a web application , then you can use the session to put the user info into the session and access the user id and persist that in the database ( make sure that you have foreign key to the user table )

If you are using Spring framework , you can look at using interceptors to access and set the user id for every database update call

Rocky
  • 941
  • 7
  • 11
  • 1
    No Spring: this is a PHP web app if you look at the tags (yii framework, php, mysql) – Ben Feb 17 '12 at 10:07
-2

Make sure your created_by and updated_by fields are DATETIME then when you run UPDATE queries against them to update the data, make sure you pass NOW() without any quotes. This will update those fields to the current DATETIME e.g. 2012-02-17 10:00:00.

Hope this helps.

arkin
  • 21
  • 2
  • 4
    It's not created_**on**, it's created_**by**. – Jon Feb 17 '12 at 10:01
  • Ok thanks @arkin for such quick reply.I want to know how to get the creator id and the person who has made updated his id in that field. – NewUser Feb 17 '12 at 10:03