Есть класс:
Код: Выделить всё
class Users extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
// User in registering process
const STATUS_REGISTERING = 0;
public $id;
public $username;
public $authKey;
public $passwordHash;
public $passwordResetToken;
public $phone;
public $status;
public $createdAt;
public $updatedAt;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username'], 'default', 'value' => '',
[['password_hash', 'phone'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['username', 'password_hash', 'password_reset_token', 'phone'], 'string', 'max' => 255],
[['auth_key'], 'string', 'max' => 32],
[['username'], 'unique'],
[['phone'], 'unique'],
[['password_reset_token'], 'unique'],
];
}
public function createNewUser(string $phone, string $password = '', string $authKey = '')
{
$model = new self();
$model->username = $phone;
$model->phone = $phone;
$model->created_at = time();
$model->status = self::STATUS_REGISTERING;
$model->auth_key = $authKey;
$model->setPassword($password);
return $model->save();
}
//...
}
Код: Выделить всё
$phone = '12221234567';
$userModel = new Users();
$userModel->createNewUser($phone);
хотя я явно указал значение для поля `username`.SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value↵The SQL being executed was: INSERT INTO `users` (`created_at`, `auth_key`, `password_hash`, `updated_at`) VALUES (1611327152, '82qnq...eiTXNR', '$2y$13$zgd...HOb1.ek2', 0)
Ощущение, что данные в методе просто игнорируются. Почему так? Что сделать, чтобы заработало?