php - 如何使用 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)。
几天来我一直在寻找解决方案,但找不到。
如果有人可以指导我,请。
解决方案
我认为只需添加 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;
}
}
我当然忘了一步...
推荐阅读
- java - 如何修复 Talend 中的“日期格式”问题
- c# - 如何在 UWP(通用 Windows 编程)应用程序中进行简单的用户登录
- curl - 无法从托管它的服务器内部 curl 到站点,它返回 403 Forbidden,但是从外部到站点使用 curl 很好
- c++ - 如何将参数 -fvisibility=default 添加到 CMAKE?
- c++ - 使用相同的参数集分离内部和外部构造函数
- extjs - 在 ExtJs 中支持单个构建下的多个主题
- javascript - Nodejs:获取从前端传递的对象的名称和值
- mysql - How to get data from 2 parent tables that are linked via a intermediary table
- python - In Sklearn, is there a clean way to transform a list of dicts?
- java - How to fix RxAndroid bug in Android