ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 12. 模型事件 ## 模型事件 如果我们想对后台文章模型或单页等模型新增、修改、删除时进行一些操作,那么我们可以在插件初始化事件中定义相应的模型事件。 事件描述AfterRead查询后BeforeInsert新增前AfterInsert新增后BeforeUpdate更新前AfterUpdate更新后BeforeWrite写入前AfterWrite写入后BeforeDelete删除前AfterDelete删除后BeforeRestore恢复前AfterRestore恢复后## 示例 监听文章模型更新后 ``` <pre class="calibre16">``` <span class="token5">// 插件初始化事件</span> public <span class="token2">function</span> <span class="token6">addonsInitHook</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> Event<span class="token1">:</span><span class="token1">:</span><span class="token6">listen</span><span class="token1">(</span><span class="token4">'model.app\admin\model\cms\Archives.AfterUpdate'</span><span class="token1">,</span><span class="token2">function</span> <span class="token1">(</span>$model<span class="token1">)</span><span class="token1">{</span> <span class="token5">// 获取没更新前的数据</span> $origin <span class="token">=</span> $model<span class="token">-</span><span class="token">></span><span class="token6">getOrigin</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 获取更新后的数据</span> $data <span class="token">=</span> $model<span class="token">-</span><span class="token">></span><span class="token6">getData</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 监听文章模型新增后事件 ``` <pre class="calibre16">``` <span class="token5">// model.模型类命名空间.事件</span> Event<span class="token1">:</span><span class="token1">:</span><span class="token6">listen</span><span class="token1">(</span><span class="token4">'model.app\admin\model\cms\Archives.AfterInsert'</span><span class="token1">,</span><span class="token2">function</span> <span class="token1">(</span>$model<span class="token1">)</span><span class="token1">{</span> <span class="token1">}</span><span class="token1">)</span> ``` ``` 更多模型监听,可前往后台模型查看 app/admin/model/