POST a request with subresource data is not working

Viewed 38

There are two tables with relations OneToOne.

But on POST data to main reqourse sub-resource is not creating:

POST /fraud-checks:

{
    "service_id": "pro",
    "requests_phone_number": {
        "phone": "+499261234567"
    }
}

Response:

{
    "@context": "/contexts/fraud-check",
    "@id": "/fraud-checks/d2aeea3e-bf23-11ec-b6da-525400bbb7dc",
    "@type": "fraud-check",
    "service_id": "0",
    "price": 0,
    "created_at": 1650292112,
    "updated_at": null,
    "requests_phone_number": null,
    "phone_hlr": null,
    "email": null,
    "id": "d2aeea3e-bf23-11ec-b6da-525400bbb7dc"
}

Request.php

<?php

declare(strict_types=1);
namespace app\antifraud\entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Request
 *
 * @ORM\Table(name="request", indexes={@ORM\Index(name="status_id", columns={"status_id"})})
 * @ORM\Entity(repositoryClass="app\antifraud\repositories\RequestRepository")
 */
class Request implements Status
{
    use StatusTrait;
    use UuidTrait;


    /**
     * @var int
     *
     * @ORM\Column(name="user_id", type="bigint", nullable=false, options={"unsigned"=true})
     */
    private $userId;

    /**
     * @var string
     *
     * @ORM\Column(name="service_id", type="string", length=50, nullable=false)
     */
    private $serviceId;

    /**
     * @var int
     *
     * @ORM\Column(name="price", type="smallint", nullable=false, options={"unsigned"=true})
     */
    private $price = '0';

    /**
     * @var int
     *
     * @ORM\Column(name="created_at", type="integer", nullable=false, options={"unsigned"=true})
     */
    private $createdAt;

    /**
     * @var int|null
     *
     * @ORM\Column(name="updated_at", type="integer", nullable=true, options={"unsigned"=true})
     */
    private $updatedAt;

    /**
     * @var int|null
     *
     * @ORM\Column(name="deleted_at", type="integer", nullable=true, options={"unsigned"=true})
     */
    private $deletedAt;

    /**
     * @var string
     *
     * @ORM\Column(name="ip", type="string", length=50, nullable=false)
     */
    private $ip;

    /**
     * @var RequestsPhoneNumber|null
     *
     * @ORM\OneToOne(targetEntity="RequestsPhoneNumber", mappedBy="request", cascade={"persist"})
     */
    private $requestsPhoneNumber;

    /**
     * @var RequestsPhoneHlr|null
     *
     * @ORM\OneToOne(targetEntity="RequestsPhoneHlr", mappedBy="request", cascade={"persist"})
     */
    private $phoneHlr;

    /**
     * @var RequestsEmail|null
     *
     * @ORM\OneToOne(targetEntity="RequestsEmail", mappedBy="request", cascade={"persist"})
     */
    private $email;

    /**
     * @return int
     */
    public function getUserId(): int
    {
        return $this->userId;
    }

    /**
     * @param int $userId
     */
    public function setUserId(int $userId): void
    {
        $this->userId = $userId;
    }

    /**
     * @return string
     */
    public function getServiceId(): string
    {
        return $this->serviceId;
    }

    /**
     * @param string $serviceId
     */
    public function setServiceId(string $serviceId): void
    {
        $this->serviceId = $serviceId;
    }

    /**
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @param int $price
     */
    public function setPrice($price): void
    {
        $this->price = $price;
    }

    /**
     * @return int
     */
    public function getCreatedAt(): int
    {
        return $this->createdAt;
    }

    /**
     * @param int $createdAt
     */
    public function setCreatedAt(int $createdAt): void
    {
        $this->createdAt = $createdAt;
    }

    /**
     * @return int|null
     */
    public function getUpdatedAt(): ?int
    {
        return $this->updatedAt;
    }

    /**
     * @param int|null $updatedAt
     */
    public function setUpdatedAt(?int $updatedAt): void
    {
        $this->updatedAt = $updatedAt;
    }

    /**
     * @return int|null
     */
    public function getDeletedAt(): ?int
    {
        return $this->deletedAt;
    }

    /**
     * @param int|null $deletedAt
     */
    public function setDeletedAt(?int $deletedAt): void
    {
        $this->deletedAt = $deletedAt;
    }

    public function getRequestsPhoneNumber(): ?RequestsPhoneNumber
    {
        return $this->requestsPhoneNumber;
    }

    public function getPhoneHlr(): ?RequestsPhoneHlr
    {
        return $this->phoneHlr;
    }

    /**
     * @return RequestsEmail|null
     */
    public function getEmail(): ?RequestsEmail
    {
        return $this->email;
    }

    /**
     * @return string
     */
    public function getIp(): string
    {
        return $this->ip;
    }

    /**
     * @param string $ip
     */
    public function setIp(string $ip): void
    {
        $this->ip = $ip;
    }

}

RequestsPhoneNumber.php

<?php

declare(strict_types=1);
namespace app\antifraud\entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * RequestsPhoneNumber
 *
 * @ORM\Table(name="requests_phone_number", indexes={@ORM\Index(name="request_id", columns={"request_id"}), @ORM\Index(name="status_id", columns={"status_id"})})
 * @ORM\Entity(repositoryClass="app\antifraud\repositories\RequestsPhoneNumberRepository")
 */
class RequestsPhoneNumber
{
    use StatusTrait;
    use UuidTrait;

    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=50, nullable=false)
     */
    private $phone;

    /**
     * @var string|null
     *
     * @ORM\Column(name="iso2", type="string", length=2, nullable=true, options={"fixed"=true})
     */
    private $iso2;

    /**
     * @var bool|null
     *
     * @ORM\Column(name="is_possible", type="boolean", nullable=true)
     */
    private $isPossible;

    /**
     * @var bool|null
     *
     * @ORM\Column(name="is_valid", type="boolean", nullable=true)
     */
    private $isValid;

    /**
     * @var string|null
     *
     * @ORM\Column(name="region", type="string", length=2, nullable=true)
     */
    private $region;

    /**
     * @var string|null
     *
     * @ORM\Column(name="number_type", type="string", length=2, nullable=true)
     */
    private $numberType;

    /**
     * @var string|null
     *
     * @ORM\Column(name="operator_name", type="string", length=255, nullable=true)
     */
    private $operatorName;

    /**
     * @var int
     *
     * @ORM\Column(name="created_at", type="integer", nullable=false, options={"unsigned"=true})
     */
    private $createdAt;

    /**
     * @var int|null
     *
     * @ORM\Column(name="updated_at", type="integer", nullable=true, options={"unsigned"=true})
     */
    private $updatedAt;

    /**
     * @var Request
     *
     * @ORM\OneToOne(targetEntity="Request", inversedBy="requestsPhoneNumber", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="request_id", referencedColumnName="id")
     * })
     */
    private $request;

    /**
     * @return string
     */
    public function getPhone(): string
    {
        return $this->phone;
    }

    /**
     * @param string $phone
     */
    public function setPhone(string $phone): void
    {
        $this->phone = $phone;
    }

    /**
     * @return string|null
     */
    public function getIso2(): ?string
    {
        return $this->iso2;
    }

    /**
     * @param string|null $iso2
     */
    public function setIso2(?string $iso2): void
    {
        $this->iso2 = $iso2;
    }

    /**
     * @return bool|null
     */
    public function getIsPossible(): ?bool
    {
        return $this->isPossible;
    }

    /**
     * @param bool|null $isPossible
     */
    public function setIsPossible(?bool $isPossible): void
    {
        $this->isPossible = $isPossible;
    }

    /**
     * @return bool|null
     */
    public function getIsValid(): ?bool
    {
        return $this->isValid;
    }

    /**
     * @param bool|null $isValid
     */
    public function setIsValid(?bool $isValid): void
    {
        $this->isValid = $isValid;
    }

    /**
     * @return string|null
     */
    public function getRegion(): ?string
    {
        return $this->region;
    }

    /**
     * @param string|null $region
     */
    public function setRegion(?string $region): void
    {
        $this->region = $region;
    }

    /**
     * @return string|null
     */
    public function getNumberType(): ?string
    {
        return $this->numberType;
    }

    /**
     * @param string|null $numberType
     */
    public function setNumberType(?string $numberType): void
    {
        $this->numberType = $numberType;
    }

    /**
     * @return string|null
     */
    public function getOperatorName(): ?string
    {
        return $this->operatorName;
    }

    /**
     * @param string|null $operatorName
     */
    public function setOperatorName(?string $operatorName): void
    {
        $this->operatorName = $operatorName;
    }

    /**
     * @return int
     */
    public function getCreatedAt(): int
    {
        return $this->createdAt;
    }

    /**
     * @param int $createdAt
     */
    public function setCreatedAt(int $createdAt): void
    {
        $this->createdAt = $createdAt;
    }

    /**
     * @return int|null
     */
    public function getUpdatedAt(): ?int
    {
        return $this->updatedAt;
    }

    /**
     * @param int|null $updatedAt
     */
    public function setUpdatedAt(?int $updatedAt): void
    {
        $this->updatedAt = $updatedAt;
    }

    /**
     * @return Request
     */
    public function getRequest(): Request
    {
        return $this->request;
    }

    /**
     * @param Request $request
     */
    public function setRequest(Request $request): void
    {
        $this->request = $request;
    }



}

resource file fraud_check_request.yaml

app\antifraud\entities\Request:
  shortName: 'fraud-check'
  description: "Fraud check request"
  collectionOperations:
    get: ~
    post:
      denormalization_context:
        groups: [ 'fraud_request_write' ]
  itemOperations:
    get: ~
    delete: ~
  attributes:
    normalization_context:
      groups: [ 'fraud_request_read' ]
    denormalization_context:
      groups: [ 'fraud_request_write' ]
  properties:
    requestsPhoneNumber:
      subresource:
        resourceClass: 'app\antifraud\entities\RequestsPhoneNumber'
        collection: false

fraud_check_request_phone_number.yaml:

app\antifraud\entities\Request:
  shortName: 'fraud-check'
  description: "Fraud check request"
  collectionOperations:
    get: ~
    post:
      denormalization_context:
        groups: [ 'fraud_request_write' ]
  itemOperations:
    get: ~
    delete: ~
  attributes:
    normalization_context:
      groups: [ 'fraud_request_read' ]
    denormalization_context:
      groups: [ 'fraud_request_write' ]
  properties:
    requestsPhoneNumber:
      subresource:
        resourceClass: 'app\antifraud\entities\RequestsPhoneNumber'
        collection: false




Serializers:

fraud_check_request.yaml:

app\antifraud\entities\Request:
  attributes:
    id:
      groups: ['fraud_request_read']
    serviceId:
      groups: ['fraud_request_read', 'fraud_request_write']
    price:
      groups: ['fraud_request_read']
    createdAt:
      groups: ['fraud_request_read']
    updatedAt:
      groups: ['fraud_request_read']
    requestsPhoneNumber:
      groups: [ 'fraud_request_read', 'fraud_request_write' ]
    status:
      groups: ['fraud_request_read']
    phoneHlr:
      groups: ['fraud_request_read']
    email:
      groups: ['fraud_request_read']

fraud_check_requests_phone_number.yaml:

app\antifraud\entities\RequestsPhoneNumber:
  attributes:
    id:
      groups: ['fraud_request_read']
    phone:
      groups: ['fraud_request_read', 'fraud_request_write']
    iso2:
      groups: ['fraud_request_read']
    isPossible:
      groups: ['fraud_request_read']
    isValid:
      groups: ['fraud_request_read']
    region:
      groups: ['fraud_request_read']
    numberType:
      groups: ['fraud_request_read']
    operatorName:
      groups: ['fraud_request_read']
    createdAt:
      groups: ['fraud_request_read']
    updatedAt:
      groups: ['fraud_request_read']
    status:
      groups: ['fraud_request_read']

API Platform 2.6 Symfony 5.4

Please help properly set-up API platform to allow to create a sub-resource.

0 Answers
Related