我尝试将 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)。
我已经寻找了好几天的解决方案,但没有找到。
如果有人可以指导我。