Allener

Allener

这家伙有点懒,还没写个性签名!

  • 财富值245
  • 威望值10
  • 总积分395

个人信息

  • 回复了 的回答

    源码参考yii\db\baseActiveRecord

    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;
        }
    

    $insert 表示是否是插入,changedAttributes显而易见可以知道,更新的时候被改变的attribute

    model中的这$insert和$changedAttributes俩变量值是控制层传来的?

  • 回复了 的评论

    呵呵

    呵呵

  • 2017-10-11 已签到
    连续签到2天,获得了10个金钱
  • 回复了 的回答

    $insert 表示是否是插入,$changedAttributes 表示修改的字段

    难道$insert说字符串而不是变量?

  • 2017-10-10 已签到
    连续签到1天,获得了5个金钱
  • 提出了问题
    请教一个afterSave的问题
  • 2017-10-08 已签到
    连续签到1天,获得了5个金钱
  • 2017-10-06 已签到
    连续签到2天,获得了10个金钱
  • 回复了 的回复

    其实Yii2中登陆login本来就已经有事件了.Yii::$app->user->login($user);登陆成功会调用afterLogin方法,触发EVENT_AFTER_LOGIN

    protected function afterLogin($identity, $cookieBased, $duration)
        {
            $this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent([
                'identity' => $identity,
                'cookieBased' => $cookieBased,
                'duration' => $duration,
            ]));
        }
    

    返回了完整的用户信息.所以事件没必要新增,UserLoginEvent也不需要了.

    Yii2中登陆login有事件了,假如现在初学者不是用的login呢?用的是lo或者og其他杂七杂八的。

  • 非常感谢作者,让我理解了事件的一些基本的操作概念。

见习主管 等级规则
395/500
资料完整度
10/100
用户活跃度
0/100

Ta的关注

2

Ta的粉丝

0

Ta的访客

4