我试图在模型的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;
}
}
Run Code Online (Sandbox Code Playgroud)
和另一个模型之前保存()
public function beforeSave(){
if(parent::beforeSave()){
if($this->isNewRecord){
$this->logtime=time();
$this->user_id=Yii::app()->user->id;
}
return true;
}else{
return false;
}
Run Code Online (Sandbox Code Playgroud)
} 谢谢.
当你尝试保存activerecord时,我可以想到Yii没有调用beforeSave的唯一原因是它的验证失败了.要么在保存调用后尝试转储$ object-> errors,要么尝试使用$ object-> save(FALSE)保存它以跳过验证.像这样我们可以消除这个原因.