我刚刚使用 symfony 和原则创建了一个实体“候选者”,您可能会在下面看到:
<
?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\CandidateRepository;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* @ApiResource(
* normalizationContext={"groups"={"user:read"}},
* denormalizationContext={"groups"={"user:write"}}
* )
* @ORM\Entity(repositoryClass=CandidateRepository::class)
*/
class Candidate implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups("user:read")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups({"user:read", "user:write"})
*/
private $email;
/**
* @ORM\Column(type="json")
* @Groups("user:read")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
* @Groups({"user:read", "user:write"})
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $name;
/**
* @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 $phone;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $address;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $city;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $zip;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $photo;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $seeking_job_type;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $seeking_job_contract;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
*/
private $availability;
/**
* @ORM\Column(type="datetime")
* @Groups({"user:read", "user:write"})
*/
private $registration_date;
/**
* @ORM\OneToOne(targetEntity=Cv::class, cascade={"persist", "remove"})
* @Groups("user:read")
*/
private $id_cv;
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 getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
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 PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getFirstName(): ?string
{
return $this->first_name;
}
public function setFirstName(string $first_name): self
{
$this->first_name = $first_name;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getZip(): ?string
{
return $this->zip;
}
public function setZip(string $zip): self
{
$this->zip = $zip;
return $this;
}
public function getPhoto(): ?string
{
return $this->photo;
}
public function setPhoto(string $photo): self
{
$this->photo = $photo;
return $this;
}
public function getSeekingJobType(): ?string
{
return $this->seeking_job_type;
}
public function setSeekingJobType(string $seeking_job_type): self
{
$this->seeking_job_type = $seeking_job_type;
return $this;
}
public function getSeekingJobContract(): ?string
{
return $this->seeking_job_contract;
}
public function setSeekingJobContract(string $seeking_job_contract): self
{
$this->seeking_job_contract = $seeking_job_contract;
return $this;
}
public function getAvailability(): ?string
{
return $this->availability;
}
public function setAvailability(string $availability): self
{
$this->availability = $availability;
return $this;
}
public function getRegistrationDate(): ?\DateTimeInterface
{
return $this->registration_date;
}
public function setRegistrationDate(\DateTimeInterface $registration_date): self
{
$this->registration_date = $registration_date;
return $this;
}
public function getIdCv(): ?Cv
{
return $this->id_cv;
}
public function setIdCv(?Cv $id_cv): self
{
$this->id_cv = $id_cv;
return $this;
}
}
正如你所看到的,我的属性:“first_name”、“seeking_job_type”和“seeking_job_contract”被设置为“@Groups({“user:read”,“user:write”})。我相信这允许它们被写入。 但在 API 资源中它们是“readOnly: true”。所以我无法操纵它们。 你知道我错在哪里吗?我希望它们不是“只读”的。 谢谢
解决了。 我必须在我的函数前添加下划线: public function setFirst_Name(string $first_name): self