如何使用 sylius 1.10 扩展客户/商店用户注册?

我尝试将 sylius api 的客户/商店用户注册字段添加到路径 /shop/customers。

我扩展了客户模型:

<?php

declare(strict_types=1);

namespace App\Entity\Customer;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Customer as BaseCustomer;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_customer")
 */
class Customer extends BaseCustomer
{
    /**
     * @ORM\Column(type="string",length=14)
     * @Groups({"shop:customer:create"})
     */
    private $siret;

    public function getSiret(): ?string
    {
        return $this->siret;
    }

    public function setSiret(string $siret): self
    {
        $this->siret = $siret;

        return $this;
    }
}

并扩展 Sylius\Bundle\ApiBundle\Command\RegisterShopUser :

    <?php


namespace App\Controller\ShopAPI\Commands;


use Sylius\Bundle\ApiBundle\Command\RegisterShopUser;

class UserRegistrationCommand extends RegisterShopUser
{
    protected string $siret;

    public function __construct(
        string $email,string $plainPassword,string $firstName,string $lastName,string $channelCode,?bool $subscribedToNewsletter,?string $phoneNumber,string $siret
    )
    {
        parent::__construct(
            $email,$plainPassword,$firstName,$lastName,$channelCode,$subscribedToNewsletter,$phoneNumber
        );
        $this->siret = $siret;
    }

    public function siret(): string
    {
        return $this->siret;
    }

    public static function fromHttpRequestAndChannel(Request $request,ChannelInterface $channel): ChannelBasedRequestInterface
    {
        return new self($request,$channel->getcode());
    }
}

我添加到 services.yaml 中:

App\Controller\ShopAPI\Commands\UserRegistrationCommand:
    arguments:
        $siret: "%siret%"

但是,当我与 Postman 一起发布带有新字段 (siret) 的 json 时,我遇到了错误 500 违反完整性约束“siret”字段不能为空 (null)。

我已经寻找了好几天的解决方案,但没有找到。

如果有人可以指导我。

zmc9556 回答:如何使用 sylius 1.10 扩展客户/商店用户注册?

如果您想允许该字段为 MonoBehavior ...

  1. 需要在数据库中允许空值,更新列注解如下:

// On main thread,during initialization: var syncContext = System.Threading.SynchronizationContext.Current; // On your worker thread syncContext.Post(_ => { // This code here will run on the main thread Debug.Log("Hello from main thread!"); },null);

  1. null * @ORM\Column(type="string",length=14,nullable=true) 中将参数类型声明更改为可空:

UserRegistrationCommand::__construct

  1. 更新数据库架构 Customer::setSiret
,

我认为只需要添加 Customer.xml : enter image description here

与:

<collectionOperations>
            <collectionOperation name="shop_post">
                <attribute name="method">POST</attribute>
                <attribute name="path">/shop/customers</attribute>
                <attribute name="openapi_context">
                    <attribute name="summary">Registers a new customer</attribute>
                </attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">shop:customer:create</attribute>
                </attribute>
                <attribute name="messenger">input</attribute>
                <attribute name="input">App\Command\RegisterShopUser</attribute>
                <attribute name="output">false</attribute>
            </collectionOperation>
        </collectionOperations>

并扩展 Sylius\Bundle\ApiBundle\Command\RegisterShopUser :

<?php

declare(strict_types=1);



namespace App\Command;

use Sylius\Bundle\ApiBundle\Command\RegisterShopUser as BaseRegisterShopUser;



/**

 * @experimental

 */

class RegisterShopUser extends BaseRegisterShopUser

{

    /**

     * @var int

     * @psalm-immutable

     */

    public $siret;



    public function __construct(

        int $siret

    ) {

        $this->siret = $siret;

    }

}

我当然忘记了一步...

本文链接:https://www.f2er.com/695.html

大家都在问