[英]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.