Rest API平台在normalizationContext和denormalizationContext时未显示名字和姓氏

Rest API平台在normalizationContext和denormalizationContext时未显示名字和姓氏,rest,symfony4,api-platform.com,Rest,Symfony4,Api Platform.com,我是API平台的新手,在API平台上我的用户实体的normalizationContext和denormalizationContext方面有一些问题。当我将normalizationContext和denormalizationContext添加到ApiResources时,是的,我在我的名字和姓氏变量上方添加了@Groups({“user:read”,“user:write”})。但是当试图从api平台发布数据时,我没有看到我的first_name和last_name变量选项 这是我的用户

我是API平台的新手,在API平台上我的用户实体的normalizationContext和denormalizationContext方面有一些问题。当我将normalizationContext和denormalizationContext添加到ApiResources时,是的,我在我的名字和姓氏变量上方添加了
@Groups({“user:read”,“user:write”})
。但是当试图从api平台发布数据时,我没有看到我的first_name和last_name变量选项

这是我的用户实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ApiResource(
 *     normalizationContext={"groups"={"user:read"}},
 *     denormalizationContext={"groups"={"user:write"}},
 * )
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Groups({"user:read", "user:write"})
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    private $email;

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     * @Groups({"user:write"})
     */
    private $password;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     */
    private $first_name;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     */
    private $last_name;

    /**
     * @ORM\Column(type="date")
     * @Groups({"user:read", "user:write"})
     */
    private $dob;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        // not needed when using the "bcrypt" algorithm in security.yaml
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getFirstName(): ?string
    {
        return $this->first_name;
    }

    public function setFirstName(string $first_name): self
    {
        $this->first_name = $first_name;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->last_name;
    }

    public function setLastName(string $last_name): self
    {
        $this->last_name = $last_name;

        return $this;
    }

    public function getDob(): ?\DateTimeInterface
    {
        return $this->dob;
    }

    public function setDob(\DateTimeInterface $dob): self
    {
        $this->dob = $dob;

        return $this;
    }
}


您需要对属性使用camelcase。这是一个默认情况。条令没有将getFirstName()识别为$first\u name的getter。而且由于属性被声明为私有api平台,因此无法获取该值

这应该起作用:

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     */
    private $firstName;    

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }
或者您可以尝试强制条令使用下划线,如下所示:

doctrine:
    orm:
        naming_strategy: doctrine.orm.naming_strategy.underscore

您需要对属性使用camelcase。这是一个默认情况。条令没有将getFirstName()识别为$first\u name的getter。而且由于属性被声明为私有api平台,因此无法获取该值

这应该起作用:

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"user:read", "user:write"})
     */
    private $firstName;    

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }
或者您可以尝试强制条令使用下划线,如下所示:

doctrine:
    orm:
        naming_strategy: doctrine.orm.naming_strategy.underscore

我想可能是名字和姓氏中的下划线。配置中有一个名为命名策略的选项,因此您可以强制条令正确读取下划线。但它必须在所有实体上保持一致。我认为它可能是名字和姓氏中的下划线。配置中有一个名为命名策略的选项,因此您可以强制条令正确读取下划线。但它必须在所有实体上保持一致