首页 > 解决方案 > 如何使用 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)。

几天来我一直在寻找解决方案,但找不到。

如果有人可以指导我,请。

标签: phpsymfonysylius

解决方案


我认为只需添加 Customer.xml : 在此处输入图像描述

和 :

<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;

    }

}

我当然忘了一步...


推荐阅读