【发布时间】:2017-03-02 01:03:37
【问题描述】:
我正在尝试在更新任何其他表中的记录时将记录插入到我的审计表中。例如,如果用户更新他的个人资料,我想将旧记录和新更新的记录存储在我的审计表中。为此,在我的用户模型中,我尝试使用 beforeSave() 并将值传递给我的审计控制器
public function beforeSave($insert)
{
if((parent::beforeSave($insert))){
// Place your custom code here
$query = DepCustomer::findOne($this->customer_id);
Yii::$app->runAction('audit-trial/createaudit', ['query' => $query]);
return true;
}
}
现在审核控制器中的操作代码
public function actionCreateaudit($query)
{
$model = new Audit();
$model->old = '';
foreach($query as $name => $value){
//$temp = $name .': '. $value.', ';
//$contentBefore[] = $temp;
$audit->old = $audit->old.$name .': '. $value. ', ';
}
// I've not yet any other code for now I'm trying to get the old value
$model->save();
}
我收到 404 not found 错误。我需要对代码进行哪些更改才能使其正常工作?谢谢!
【问题讨论】:
-
你在“createaudit”这个动作中有什么特殊的逻辑吗,如果你不为什么不在 beforesave 方法中保存查询。如果您有特殊逻辑,最好将其移至模型“AuditTrial”,然后您可以从代码中的任何位置调用它
-
是的!这是真的。我可以在 beforesave 方法或客户控制器的更新操作中保存新旧更新数据,我已经实现了这一点。现在我也在尝试保存其他模型的审计记录。所以我正在尝试编写代码以在 beforesave 方法中传递旧值,以便我也可以将代码放入其他模型中。