本文介绍了API平台在normalizationContext和denormalizationContext中未显示名字和姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是API平台的新手,我在API平台上的用户实体在normalizationContext和denormalizationContext方面存在一些问题.当我将normalizationContext和denormalizationContext添加到ApiResources时,是的,我确实在first_name和last_name变量上方以 @Groups({"user:read","user:write"})添加.但是,当尝试从api平台发布数据时,我没有看到我的first_name和last_name变量选项.

I am new to API Platform and I have some problem with normalizationContext and denormalizationContext for my User Entity on the API platform. When I added the normalizationContext and denormalizationContext to ApiResources, and yes I did add in as @Groups({"user:read", "user:write"}) above my first_name and last_name variable. But when trying to post data from the api platform, I didn't see my first_name and last_name variable option.

这是我的用户实体

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

我希望为读写组提供API平台的first_name和last_name变量.我该如何解决?

I wanted to available first_name and last_name variable to the API platform for both read and write group. How can I fix this?

推荐答案

您需要使用camelcase作为属性.教义默认情况下期望驼峰式.Doctrine没有将getFirstName()当作$ first_name的获取方法.而且由于该属性已声明为私有,因此api平台无法获取该值.

You need to use camelcase for properties. Doctrine expect camelcase by default. Doctrine didn't recognize getFirstName() as the getter for $first_name. And since the property is declared private api-platform is not able to get the value.

这应该起作用:

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

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

或者您也可以像这样尝试强迫学说使用下划线:

or you can try to force doctrine to use underscore like so:

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

这篇关于API平台在normalizationContext和denormalizationContext中未显示名字和姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:14