Yii2 Active Record 的生命周期

2019-12-17 20:41:58 浏览数 (1)

Yii2 Active Record 的生命周期

当你实现各种功能的时候,会发现了解 Active Record 的生命周期很重要。 在每个生命周期中,一系列的方法将被调用执行,您可以重写这些方法 以定制你要的生命周期。您还可以响应触发某些 Active Record 事件 以便在生命周期中注入您的自定义代码。这些事件在开发 Active Record 的 行为时特别有用, 通过行为可以定制 Active Record 生命周期的 。

Active Record 的生命周期 大致划分为以下几种:

  • 实例化生命周期(New Instance Life Cycle)
  • 查询数据生命周期(Querying Data Life Cycle)
  • 保存数据生命周期(Saving Data Life Cycle)
  • 删除数据生命周期(Deleting Data Life Cycle) <
  • 刷新数据生命周期(Refreshing Data Life Cycle)

以上内容来自 Yii2.0权威指南 ,更多介绍请参考 Active Record 的生命周期

以下为 Acative Record 的各种事件

代码语言:javascript复制
/**
 * @event Event an event that is triggered when the record is initialized via [[init()]].
 */
const EVENT_INIT = 'init';
/**
 * @event Event an event that is triggered after the record is created and populated with query result.
 */
const EVENT_AFTER_FIND = 'afterFind';
/**
 * @event ModelEvent an event that is triggered before inserting a record.
 * You may set [[ModelEvent::isValid]] to be `false` to stop the insertion.
 */
const EVENT_BEFORE_INSERT = 'beforeInsert';
/**
 * @event AfterSaveEvent an event that is triggered after a record is inserted.
 */
const EVENT_AFTER_INSERT = 'afterInsert';
/**
 * @event ModelEvent an event that is triggered before updating a record.
 * You may set [[ModelEvent::isValid]] to be `false` to stop the update.
 */
const EVENT_BEFORE_UPDATE = 'beforeUpdate';
/**
 * @event AfterSaveEvent an event that is triggered after a record is updated.
 */
const EVENT_AFTER_UPDATE = 'afterUpdate';
/**
 * @event ModelEvent an event that is triggered before deleting a record.
 * You may set [[ModelEvent::isValid]] to be `false` to stop the deletion.
 */
const EVENT_BEFORE_DELETE = 'beforeDelete';
/**
 * @event Event an event that is triggered after a record is deleted.
 */
const EVENT_AFTER_DELETE = 'afterDelete';
/**
 * @event Event an event that is triggered after a record is refreshed.
 * @since 2.0.8
 */
const EVENT_AFTER_REFRESH = 'afterRefresh';

0 人点赞