[英]Could not determine access type
我需要創建一個名為 Users 的主實體和另一個名為 Specialties 的主實體。 一個用戶可以有很多專長。
有我的用戶實體:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UsersRepository")
* @UniqueEntity(
* fields={"email"},
* errorPath="email",
* message="It appears you have already registered with this email."
* )
*/
class Users implements UserInterface
{
/**
* Users constructor.
*/
public function __construct()
{
$this->created_at = new \DateTime();
$this->specialtyId = new ArrayCollection();
}
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"public"})
*/
private $id;
//Other properties there...
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Specialties", inversedBy="users")
*/
private $specialtyId;
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|Specialties[]
*/
public function getSpecialtyId(): Collection
{
return $this->specialtyId;
}
public function addSpecialtyId(Specialties $specialtyId): self
{
if (!$this->specialtyId->contains($specialtyId)) {
$this->specialtyId[] = $specialtyId;
}
return $this;
}
public function removeSpecialtyId(Specialties $specialtyId): self
{
if ($this->specialtyId->contains($specialtyId)) {
$this->specialtyId->removeElement($specialtyId);
}
return $this;
}
}
有我的專業實體:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\SpecialtiesRepository")
*/
class Specialties
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
//Other properties there...
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Users", mappedBy="specialtyId")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @return Collection|Users[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(Users $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addSpecialtyId($this);
}
return $this;
}
public function removeUser(Users $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
$user->removeSpecialtyId($this);
}
return $this;
}
}
當我嘗試保存注冊表時,出現以下異常:
**> 無法確定類中屬性“specialtyId”的訪問類型
“App\\Entity\\Users”:類“App\\Entity\\Users”中的屬性“specialtyId”可以使用“addSpecialtyId()”、“removeSpecialtyId()”方法定義,但新值必須是數組或實例\\Traversable,給出了“App\\Entity\\Specialties”。**
創建一個名為:users_specialties 的新表,其中包含:users_id/specialties_id
在用戶實體上試過這個,但仍然是同樣的錯誤:
public function addSpecialtyId(Specialties $specialtyId): self
{
if (!$this->specialtyId->contains($specialtyId)) {
$this->specialtyId[] = $specialtyId;
}
return $this;
}
public function removeSpecialtyId(Specialties $specialtyId): self
{
if ($this->specialtyId->contains($specialtyId)) {
$this->specialtyId->removeElement($specialtyId);
}
return $this;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.