yii2
新增两种方式
- 这种方式表字段少还好,如果表字段特别多,很反感这种写法。
$model = new User();
$model->name = 'james';
$model->age = 25;
$model->save();
- 块赋值
1. 定义场景
public function scenarios()
{
return [
"login" => ["username"],
"register" => ["username", "password"],
];
}
$user = new User();
$user->scenario = "login";
$user->attributes = $attr;
$user->save();
2. 通过规则
public function rules()
{
return [
[["username", "password"], "safe"]
];
}
$user = new User();
$user->attributes = $attr;
$user->save();