I try to save a registerd user in the database with an api call.
So I have an model User.php:
<?php
namespace appmodels;
use Yii;
use yiibaseNotSupportedException;
use yiibehaviorsTimestampBehavior;
use yiidbActiveRecord;
use yiiwebIdentityInterface;
class User extends yiidbActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_INACTIVE = 9;
const STATUS_ACTIVE = 10;
public function behaviors()
{
return [
TimestampBehavior::class,
];
}
public static function tableName()
{
return '{{%user}}';
}
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
];
}
}
and an controller UserController.php:
<?php
// phpcs:ignoreFile
namespace appmodulesv1controllers;
use Yii;
use appmodelsUser;
use yiirestActiveController;
use yiiwebResponse;
class UserController extends ActiveController
{
public $modelClass = 'appmodelsUser';
public function actionSignup()
{
$user = new User();
$user->load(Yii::$app->getRequest()->getBodyParams(), '');
if ($user->save()) {
Yii::$app->response->statusCode = 201;
return ['status' => 'success', 'data' => 'User registered successfully'];
} else {
Yii::$app->response->statusCode = 400;
return ['status' => 'error', 'data' => $user->errors];
}
}
and urlManager in web.php looks:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => ['class' => 'yiirestUrlRule', 'controller' => 'v1/user', 'POST' => 'v1/user/signup'],
]
and databse schema looks:
$this->createTable('{{%user}}', [
'id' => $this->primaryKey(),
'username' => $this->string()->notNull()->unique(),
'auth_key' => $this->string(32)->notNull(),
'verification_token'=> $this->string()->defaultValue(null),
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(10),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
]);
But if I try to call the api call in postman:
http://localhost:8080/v1/user/signup with json object:
{
"username": "example_user",
"password_hash": "example_password",
"email": "[email protected]"
}
I get this error:
"name": "Integrity constraint violation",
"message": "SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "username" of relation "user" violates not-null constraintnDETAIL: Failing row contains (3, null, null, null, null, null, 10, 1710925298, 1710925298, null).nThe SQL being executed was: INSERT INTO "user" ("status", "created_at", "updated_at") VALUES (10, 1710925298, 1710925298) RETURNING "id"",
Question: how to resolve this error?