0

如果我在我的模型中使用defineAuditFields,我会得到错误 ->exception("Method is not defined for this object", "Logic")

4.2.1 中不推荐使用 defineAuditFields() 吗?

有新方法吗?

4

1 回答 1

0

defineAuditFields 是旧 MVC 模型的产物。新的敏捷工具包允许您使用控制器来做同样的事情。Janis 在他的迁移指南中概述了这一点:http: //www.ambienttech.lv/blog/说您现在可以使用控制器。

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');
    }
}

当然你可以在这里做更多的动作。如果您需要软删除,那么这样的事情会很好用:

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
    }
}

ps 如果我这里的代码包含一些小错误,请[编辑]它们。

于 2012-06-14T12:23:41.247 回答