Audittrail2 Question...

I have the extension working great. The issue I am trying to overcome is if you have a drop down list for example and you pulled this from another table with id being the FK value. How would I convert this to the value instead of the FK id that is being saved in the auditTrail DB table? I would like new value / old value to be, for example, Admin - User instead of 1 - 4.

http://www.yiiframework.com/extension/audittrail2/

Thanks in advance for your help.

If you want the semantic result of the relation you will probably need to code the extension a little.

Hmmm, I had to solve this once, I chose to do this outside of the extension within the display of the audit so that the client side changed 1 to Admin or 4 to User

sammaye, do you have what you did to solve it. The only thing I can think of to do is to findByPk on old value and set the old value to the name and not the key. The new value is easy. I added an array to the extension called relation = array(‘relation’=>array(‘relation_name.field’=>‘foreign_key’).

Thanks for your response.

sammaye,

Any way to store the old model so I can access the old relations fields in it instead of querying for each one with findByPk?

Thanks.

The best way I solved this was to actually manually query for the relations when I displayed the audit trail, i.e.:




	'columns'=>array(

		array(

			'name' => 'Author',

			'value' => '$data->user ? $data->user->email : ""'

		),

		'model',

		'model_id',

		'action',

		array(

			'name' => 'field',

			'value' => '$data->resolve($data->field) instanceof CActiveRecord?$data->resolve($data->field)->name:$data->resolve($data->field)'

		),

		'old_value',

		'new_value',

		array(

			'name' => 'Date Changed',

			'value' => 'date("d-m-Y H:i:s", strtotime($data->stamp))'

		)

	),



And resolve would basically return either the relation or the field and the instanceof would check that.

Where from? Which file are you working in here?

I am working in the LoggingeBehavior file. I was trying to store the old model into a variable beforeSave but it doesn’t seem to store the old one, just the new one.

If you look above there is a point where I actually get the old attributes. There you can take the old model and save it in its old state, however, you might alreayd be doing that and instead seeing a copy on write scenario whereby even though you assigned that “copy” of the variable to another it didn’t save the state due to copy on write.

In this case you need to clone: http://php.net/manual/en/language.oop5.cloning.php