ActiveRecord的事件是在哪里注册的 [ 未指定版本 ]
我们知道ActiveRecord有EVENT_BEFORE_INSERT、EVENT_AFTER_INSERT这些事件,但是这些事件是在哪里注册的呢?我指的是源码中哪里注册了这些事件,请问有人知道吗??
共 3 个回答
-
-
johnny1991 回答于 2017-09-20 15:19 举报
yii\db\baseActiveRecord
public function afterSave($insert, $changedAttributes) { $this->trigger($insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE, new AfterSaveEvent([ 'changedAttributes' => $changedAttributes ])); }
调用
protected function updateInternal($attributes = null) { if (!$this->beforeSave(false)) { return false; } $values = $this->getDirtyAttributes($attributes); if (empty($values)) { $this->afterSave(false, $values); return 0; } $condition = $this->getOldPrimaryKey(true); $lock = $this->optimisticLock(); if ($lock !== null) { $values[$lock] = $this->$lock + 1; $condition[$lock] = $this->$lock; } // We do not check the return value of updateAll() because it's possible // that the UPDATE statement doesn't change anything and thus returns 0. $rows = static::updateAll($values, $condition); if ($lock !== null && !$rows) { throw new StaleObjectException('The object being updated is outdated.'); } if (isset($values[$lock])) { $this->$lock = $values[$lock]; } $changedAttributes = []; foreach ($values as $name => $value) { $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null; $this->_oldAttributes[$name] = $value; } $this->afterSave(false, $changedAttributes); return $rows; }
共 2 条回复johnny1991 回复于 2017-09-20 20:58 回复@是啊俊 系统没有监听这个事件,系统提供这个触发这个事件,是给开发者监听的
-
是啊俊
注册时间:2017-07-28
最后登录:2018-01-23
在线时长:13小时58分
最后登录:2018-01-23
在线时长:13小时58分
- 粉丝0
- 金钱785
- 威望20
- 积分1115