Yii在其他模型的beforeSave()中调用beforeSave()

时间:2012-09-03 15:52:48

标签: yii

我试图在模型的beforeSave()方法中保存另一个模型的记录。我期望save()方法调用另一个模型beforeSave(),但它没有。它是一个bug还是我只是错过了什么?这是一些代码。

运营模式

public function beforeSave(){
    if(parent::beforeSave()){

        if($this->isNewRecord){
            $this->creation_date=$this->modification_date=time();
            $cur=Currencies::model()->find('LOWER(short)=?',array('usd'));
            if($cur->id!=$this->currency_id){
                $conv_cours=Currencies::model()->findByPk($this->currency_id);
                $this->ammount_usd=round(($this->ammount*$conv_cours->buy)/$cur->sell,4);
            }else{
                $this->ammount_usd=$this->ammount;
            }

        }else{
            $this->modification_date=time();
        }

        $opType=OperationType::model()->findByPk($this->operation_type);

        $log=new ActionLogs;
        $log->comment=Yii::app()->user->name.' добавил '.$opType->name;
        /*$log->logtime=time();
        $log->user_id=Yii::app()->user->id;*/
        $v=1;
        $log->save ();

        return true;

    }else{
        return false;

    }

}

另一个模型beforeSave()

    public function beforeSave(){
if(parent::beforeSave()){
    if($this->isNewRecord){
        $this->logtime=time();
        $this->user_id=Yii::app()->user->id;
    }
    return true;
}else{
    return false;
}

} 谢谢。

1 个答案:

答案 0 :(得分:6)

当你尝试保存activerecord时,我想到Yii没有调用beforeSave的唯一原因是它的验证失败了。在保存调用后尝试转储$ object->错误或尝试使用$ object-> save(FALSE)保存以跳过验证。就像我们可以消除这个原因一样。