Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set validation conditions and fields default value in redbeanphp in RESTful API mode? #952

Open
exqmjmz opened this issue Oct 25, 2024 · 0 comments

Comments

@exqmjmz
Copy link

exqmjmz commented Oct 25, 2024

I have a RESTful API project based on slim framework and redbeanphp ORM.
I can already implement API CRUD.
I'm having a problem now.
I cannot implement the default field settings and field validation of redbeanphp according to the redbeanphp documentation and stackoverflow posts.

https://redbeanphp.com/index.php?p=/models

https://stackoverflow.com/questions/45575551/redbeanphp-how-to-add-timestamp-unique-and-default

PHP version:8.3
slim Version:4.*
redbean Version:5.7

Repository\UserCreatorRepository.php

<?php
namespace App\Service\User\Repository;
use App\Service\User\Data\UserCreateData;
use RedBeanPHP\R;
use RedBeanPHP\SimpleModel;

final class UserCreatorRepository extends SimpleModel
{
    public function update(): void
    {
        if (strlen($this->bean->firstName )<10)
            die("Note is too short!\n");
    }
    public function testinsert(UserCreateData $userCreateData): array
    {
        // 切换到 db1 数据库
        R::selectDatabase('user');
        // 创建新用户
        $user = R::dispense('user');
        $user->username = $userCreateData->username;
        $user->email = $userCreateData->email;
        $user->lastName = $userCreateData->lastName;
        $user->firstName = $userCreateData->firstName;
        // 保存用户
        $id = R::store($user);
        // 获取插入的用户数据
        return R::load('user', $id)->export();
    }
}

or
Repository\UserCreatorRepository.php

<?php
namespace App\Service\User\Repository;
use App\Service\User\Data\UserCreateData;
use RedBeanPHP\R;
use RedBeanPHP\SimpleModel;

final class UserCreatorRepository
{
    public function testinsert(UserCreateData $userCreateData): array
    {
        // 切换到 db1 数据库
        R::selectDatabase('user');
        // 创建新用户
        $user = R::dispense('user');
        $user->username = $userCreateData->username;
        $user->email = $userCreateData->email;
        $user->lastName = $userCreateData->lastName;
        $user->firstName = $userCreateData->firstName;
        // 保存用户
        $id = R::store($user);
        // 获取插入的用户数据
        return R::load('user', $id)->export();
    }
}

Model\User.php

<?php
namespace App\Service\User\Model;

use RedBean_SimpleModel;

class User extends RedBean_SimpleModel
{
    public function dispense(): void
    {
        if (strlen($this->bean->firstName )<10)
            die("Note is too short!\n");
    }
}

My src directory structure

My src directory structure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant