0

If I use defineAuditFields in my models I get the error ->exception("Method is not defined for this object", "Logic")

is defineAuditFields() deprecated in 4.2.1?

Is there a new method?

mcanedo
  • 445
  • 4
  • 10

1 Answers1

0

defineAuditFields was an artifact from old MVC model. The new Agile Toolkit allows you to use controllers to do the same. Janis have outlined that in his migration guide: http://www.ambienttech.lv/blog/ saying you can now use controllers.

class Controller_Audit extends AbstractController {
    function init(){
        parent::init();
        $this->owner->hasOne('User','created_by')->system(true);
        $this->owner->hasOne('created_dts')->type('datetime')->system(true);
        $this->owner->hasOne('modified_dts')->type('datetime')->system(true);

        $this->owner->addHook('beforeInsert,beforeModify',$this);
    }

    function beforeInsert($m){
        $m['created_by']=$this->api->auth->model->id;
        $m['created_dts']=date('Y-m-d H:i:s');
    }

    function beforeModify($m){
        $m['modified_dts']=date('Y-m-d H:i:s');
    }
}

Certainly you can do more actions here. If you are in a need of soft-delete, then something like this would work well:

class Controller_SoftDelete extends AbstractController {
    function init(){
        parent::init();
        $this->owner->hasOne('deleted')
            ->type('boolean')->enum(array('Y','N'))
            ->system(true);

        $this->owner->addCondition('deleted',false);

        $this->owner->addHook('beforeDelete',$this);
    }

    function beforeDelete($m,$q){
        $q->set('deleted','Y')->update();

        $q->where('1=2'); // avoid actual deletion
    }
}

p.s. if my code here contains some minor mistakes, please [edit] them.

romaninsh
  • 10,606
  • 4
  • 50
  • 70