Why $model->save() Fails in Yii2 and How to Fix It
Switching from createCommand() to the ActiveRecord pattern is a significant upgrade for maintainability, but it introduces a strict contract: data must be valid before it persists. The confusion you are experiencing stems from how Yii's validation lifecycle interacts with the database save operation. When $model->save() returns false, it almost always means one of two things occurred: a rules() definition was violated, or a column assigned in code did not pass the framework's internal checks.
In your specific scenario, the issue lies in the discrepancy between how you populate the model and what the rules() array expects. You are manually assigning properties like $model->auth_key, $model->password, and $model->created_at after calling $model->load(). However, for these assignments to be saved, they must adhere to the validation constraints defined in your model.
The Validation Lifecycle Trap
The core of the problem is that you are bypassing the standard validation step or defining rules that conflict with manual property assignment. When you call $model->save() without arguments (or with false), Yii performs a full validation cycle. If any rule returns an error, the save fails silently, and you need to check $model->hasErrors().
Your current logic attempts to assign values directly:
$model->auth_key = $auth_key;
$model->password = $password;
// ... other assignments
if($model->save()) { ... }
This approach fails if the rules() array contains a required check for these fields, or if a column is marked as safe but not properly handled. Specifically, look at this rule in your model:
[['auth_key','registration_ip','created_at'],'safe']
Marking attributes as safe is correct for scenarios where you are filling the entire model via load(), but it does not override the fact that these fields might be subject to other validation rules. If you have a rule like ['auth_key', 'required'] elsewhere in your rules() array, saving will fail because the value assigned manually might be empty or invalid according to other constraints.
However, the more immediate red flag is the handling of the password. You are hashing the password using md5():
$password = md5($post['password']);
$model->password = $password;
If your database schema defines the password column as NOT NULL, and if your validation rules implicitly require a password to be present (which they do via 'passwordRequired'), you must ensure the assignment happens within a valid state.
Correcting the Implementation
To resolve this, you should explicitly call $model->validate() before saving. This separates the logic of "is the data correct?" from "let's write to the database." Additionally, ensure that attributes assigned manually are treated correctly by the validation rules.
Here is the corrected flow for your actionRegister method:
public function actionRegister() {
$model = new Users();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
// 1. Check for existing users BEFORE saving
$CheckExistingUser = Users::findOne(['email' => $model->email]);
if (!$CheckExistingUser) {
// Prepare data manually assigned
$auth_key = $model->getConfirmationLink();
$password = md5($post['password']); // Ensure $post variable is defined correctly here
$registration_ip = Yii::$app->getRequest()->getUserIP();
$created_at = date('Y-m-d h:i:s');
// Assign properties directly to the model instance
$model->auth_key = $auth_key;
$model->password = $password;
$model->registration_ip = $registration_ip;
$model->created_at = $created_at;
// CRITICAL: Explicitly validate before saving
if ($model->validate()) {
if ($model->save())
