3

モデルの beforeSave() メソッドで別のモデルのレコードを保存しようとしています。save() メソッドが Save() の前に他のモデルを呼び出すことを期待していましたが、そうではありません。これはバグですか、それとも単に何かを見逃しているのでしょうか?ここにいくつかのコードがあります。

運用モデル

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

} ありがとう。

4

1 に答える 1

6

activerecord を保存しようとするときに Yii が beforeSave を呼び出さない唯一の理由は、その検証が失敗した場合です。save 呼び出しの後に $object->errors のダンプを試みるか、検証をスキップするために $object->save(FALSE) で保存してみてください。というように、これを原因として排除することができます。

于 2012-09-03T21:39:59.333 に答える