Yii的save方法: 下面是save方法的源码
public function save($runValidation=true,$attributes=null) { if(!$runValidation || $this->validate($attributes)) return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes); else return false; } save 方法有两个参数,第一个参数是 bool 类型,表示是否对传来的参数进行验证,第二个参数表示存储的参数数组,默认为空,表示传来的参数全部都将被存储。 如果 $runValidation 为 false ,那么就直接执行插入或更新,否则$this->validate($atttributes)过后返回 true ,再执行插入或更新,否则返回 false 。假设数据为新,那么接下来就是做 insert 操作。由于 insert 方法较长,这里截取关键语句$command=$builder->createInsertCommand($table,$this->getAttributes($attributes));我们看到通过 getAttributes 来获得参数列表,我们在 CModel 里看下 getAttributes 方法的定义: public function getAttributes($names=null) { $values=array(); foreach($this->attributeNames() as $name) $values[$name]=$this->$name; if(is_array($names)) { $values2=array(); foreach($names as $name) $values2[$name]=isset($values[$name]) ? $values[$name] : null; return $values2; } else return $values; } 如果 $attributes 如果为空,则通过 $this->attributeNames() 获得全部合法参数,否则则获得 $attributes 列表中指定的参数。save方法的第二个参数,可以指定需要更新的参数列,源码中默认为null。为全部更新