2016-11-07 41 views

回答

2

要做到这一点你必须手动处理保存过程中使用的模型类。

1-打开你的模型类。

2-写模型偶函数等public function afterSave(){}

例如,如果我在reservation模型具有total字段和我想此字段值被自动地在另一个表中插入称为accounting

public function afterSave(){ 
    $accounting = \Namespace\Pluginname\Models\Accounting::find(1); 
    $accounting->myField = $this->total; 
    $accounting->save(); 
} 

左右的时间内afterSavebeforeSavebeforeCreateafterCreate可以处理模型的保存过程。

要查看这些功能之间的区别,你可以阅读更多此链接:https://octobercms.com/docs/database/model#events

+0

很好的回答。尽管在这个通用的场景中,我可能会提出一种不同的方式来保存其他数据,而不是''find(1)'''(它只是找到id = 1的模型,否则失败,或者)'''findOrCreate(['reservation'=> $ this-> id])'''保存在与这个特定'''reservation'''相关的行中,给出一个更“现实”的例子,并指向[Laravel/Elouent docs](https://laravel.com/docs/5.3/eloquent#other-creation-methods)进行一些更深入的挖掘,... – trollkotze

+0

.. 。还有[关系](https://octobercms.com/docs/database/relations)和[关系行为](https://octobercms.com/docs/backend/relations#configuring-relation),这可能是在这种情况下更适合使用。 尽管smartassing在这里,但是:P,并试图给像我这样的其他newbiews一些额外的指针,这可能是进一步阅读的好。 – trollkotze

+0

很好的例子。当然'find(1)'是他可以编写的代码来访问应用程序中的任何模型的一个非现实的例子:D –

相关问题