cornernote / yii-audit-module

Track and display usage information including page requests, database field changes, php errors and yii logs.
https://cornernote.github.io/yii-audit-module
Other
22 stars 13 forks source link

How to track field changes? #7

Closed dadinugroho closed 10 years ago

dadinugroho commented 10 years ago

Hi cornernote,

All of my modules are inherited from MyActiveRecord. Since, I always have status field and use RememberFilterScenario.

Besides, the setting I made in main.php, I add the AuditFieldBehavior in MyActiveRecord. below is MyActiveRecord

<?php

class MyActiveRecord extends CActiveRecord {

    const INACTIVE = 0;
    const ACTIVE = 1;

    /**
     * Returns the available options of Status.
     * @return Status options
     */
    public function getStatusOptions() {
        return array(
            self::INACTIVE => 'Non-aktif',
            self::ACTIVE => 'Aktif',
        );
    }

    /**
     * Get the Status name.
     * @return Status's name
     */
    public function getStatusName($status) {
        $options = $this->getStatusOptions();

        return isset($options[$status]) ? $options[$status] : "unknown ({$status})";
    }

    public function getFormattedStatusName() {
        return TbHtml::labelTb($this->getStatusName($this->status), array('color' => ($this->status == self::ACTIVE ? TbHtml::LABEL_COLOR_SUCCESS : TbHtml::LABEL_COLOR_DEFAULT)));
    }

    public function behaviors() {
        return array(
            'ERememberFiltersBehavior' => array(
                'class' => 'application.components.ERememberFiltersBehavior',
                'defaults' => array('status' => self::ACTIVE), /* optional line */
                'defaultStickOnClear' => true /* optional line */
            ),
            'AuditFieldBehavior' => array(
                // Path to AuditFieldBehavior class.
                'class' => 'audit.components.AuditFieldBehavior',
                // Set to false if you just want to use getDbAttribute and other methods in this class.
                // If left unset the value will come from AuditModule::enableAuditField
                'enableAuditField' => null,
                // Any additional models you want to use to write model and model_id audits to.  If this array is not empty then
                // each field modifed will result in an AuditField being created for each additionalAuditModels.
                'additionalAuditModels' => array(
//                    'Post' => 'post_id',
                ),
                // A list of values that will be treated as if they were null.
                'ignoreValues' => array('0', '0.0', '0.00', '0.000', '0.0000', '0.00000', '0.000000', '0000-00-00', '0000-00-00 00:00:00'),
            ),
        );
    }

    public function scopes() {
        return array(
            'active' => array(
                'condition' => 'status = :stat',
                'params' => array(':stat' => self::ACTIVE),
            ),
            'activeSorted' => array(
                'condition' => 't.status = :stat',
                'params' => array(':stat' => self::ACTIVE),
                'order' => 't.name ASC',
            ),
            'inactive' => array(
                'condition' => 'status = :stat',
                'params' => array(':stat' => self::INACTIVE),
            ),
        );
    }

}

?>

What would be the correct setting? At the moment, I only have audit_log as the additional table after installing audit module. When I made changes to any of the model that inherits MyActiveRecord, no new table is added and checking in myinv-app.lan/audit in field tab, no entries.

Thanks a lot for your great support.

cornernote commented 10 years ago

Hi @dadinugroho,

Seems you discovered a bug that occurred when I turned this code into a module.

Please download the latest version and try again. If you have success I'll release a new version.

Thanks for your time reporting this.

cornernote commented 10 years ago

The link to download the latest (dev version) is here: https://github.com/cornernote/yii-audit-module/archive/master.zip

dadinugroho commented 10 years ago

I have tried that. Still nothing in the field tabs. Should I put anything in here, 'additionalAuditModels' => array( ....?

cornernote commented 10 years ago

Try comment out 'enableAuditField' => null,.

It's supposed to put rows into audit_field table.

dadinugroho commented 10 years ago

Yes, it works. Can I close this issue, or do you expect other result?

Thanks, mate. I am really appreciate your great support.

cornernote commented 10 years ago

Awesome. I pushed a new release, but I'll have to look into why it doesn't work when 'enableAuditField' => null, is uncommented.

cornernote commented 10 years ago

Ahh, I see the issue. I was setting enableAuditField to the value of AuditModule::enableAuditField in the constructor, however that gets overwritten when the behavior is attached. Pushing a fix now.