2017-10-08 14:03:59 4446次浏览 4条回答 0 悬赏 35 金钱
public function afterSave($insert, $changedAttributes)
{
    if (parent::afterSave($insert, $changedAttributes)){
    }
}

$insert, $changedAttributes这两个变量的意义是什么?为什么
if (parent::afterSave($insert, $changedAttributes))
这判断中必须有这两个参数。这俩参数是Yii中哪个传的?

  • 回答于 2017-10-09 00:25 举报

    源码参考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

    2 条回复
    回复于 2017-10-11 18:35 回复

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

    回复于 2017-10-11 23:07 回复

    $insert = true,表示插入,false,表示更新; 更新的时候,你提交的form表达数据写入到activeRecord对象里面,系统会对比那些字段更改了,那些字段没有更改,把更改过的字段记录下来,就是changeAttributes(不需要你生成),如果不懂,看看源码,源码也很简单的

    觉得很赞
  • 回答于 2017-10-09 11:03 举报

    $changedAttributes 这个

  • 回答于 2017-10-09 13:40 举报

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

    2 条回复
    回复于 2017-10-10 13:04 回复

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

    回复于 2017-10-10 16:04 回复

    是变量呀,因为有可能是新增,有可能是编辑,所以还是是一个变量

  • 回答于 2017-10-10 17:06 举报

    貌似楼主的写法不太对,afterSave返回的是void啊(beforeSave才有true/false),应该

    public function afterSave($insert, $changedAttributes)
    {
      if ($insert) {
        //是 插入 INSERT
      } else {
        //是 修改 UPDATE
        // 注意,如果你数据库类型是 int,那么传过来 $changedAttributes 中是字符串,也被认为是修改的属性了!!例如:
        // 周次 week 你数据库是 整数2,而 $changedAttributes['week'] 是字符串 '2',也会记录在 $changedAttributes数组中
      }
      return parent::afterSave($insert, $changedAttributes);  // 传递给父类
    }
    
    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
Allener
见习主管

Allener

注册时间:2017-10-04
最后登录:2017-11-15
在线时长:5小时46分
  • 粉丝0
  • 金钱245
  • 威望10
  • 积分395

热门问题