Symfony EntityType表单返回字符串而不是对象
我有一个与另一个实体角色有多对多关系的用户实体 我有一个表单,它允许我创建一个新用户,为此,我为许多关系“角色”使用EntityType 这是我的表格Symfony EntityType表单返回字符串而不是对象,symfony,Symfony,我有一个与另一个实体角色有多对多关系的用户实体 我有一个表单,它允许我创建一个新用户,为此,我为许多关系“角色”使用EntityType 这是我的表格 $builder ->add('username',null,['label' => 'Email']) ->add('password',null,['label'=>'Password']) ->add('roles',EntityType:
$builder
->add('username',null,['label' => 'Email'])
->add('password',null,['label'=>'Password'])
->add('roles',EntityType::class, [
'multiple' => true,
'class' => Role::class,
'choice_label' => 'role_name',
])
;
一切正常,但当我提交表单时,出现以下错误:
属性路径“角色”处给定的类型应为“App\Entity\Role”、“string”的参数
编辑:
这是我的用户实体:
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
*/
private $password;
/**
* @ORM\ManyToMany(targetEntity=Role::class, inversedBy="users")
*/
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getRoles()
{
return ['ROLE_ADMIN'];
}
public function getSalt()
{
return null;
}
public function eraseCredentials()
{
}
public function addRole(Role $role): self
{
if (!$this->roles->contains($role)) {
$this->roles[] = $role;
$role->addUser($this);
}
return $this;
}
public function removeRole(Role $role): self
{
if ($this->roles->contains($role)) {
$this->roles->removeElement($role);
$role->removeUser($this);
}
return $this;
}
}
<?php
namespace App\Entity;
use App\Repository\RoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=RoleRepository::class)
*/
class Role
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $role_name;
/**
* @ORM\ManyToMany(targetEntity=User::class, mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getRoleName(): ?string
{
return $this->role_name;
}
public function setRoleName(string $role_name): self
{
$this->role_name = $role_name;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
return $this;
}
}
我想问题就在这里
public function getRoles()
{
return ['ROLE_ADMIN'];
}
您应该返回数组或角色实体,而不是字符串,它应该是这样的
public function getRoles()
{
return $this->roles;
}
我想问题就在这里
public function getRoles()
{
return ['ROLE_ADMIN'];
}
您应该返回数组或角色实体,而不是字符串,它应该是这样的
public function getRoles()
{
return $this->roles;
}
如果我在用户实体中使用ArrayCollection怎么办
/**
* @var Collection|Role[]
* @ORM\ManyToMany(targetEntity="App\Entity\Role")
* @ORM\JoinTable(
* name="user_roles",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;
}
如果我在用户实体中使用ArrayCollection怎么办
/**
* @var Collection|Role[]
* @ORM\ManyToMany(targetEntity="App\Entity\Role")
* @ORM\JoinTable(
* name="user_roles",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;
}
你能用角色的一部分显示用户实体吗?当然,我编辑了问题。你能用角色的一部分显示用户实体吗?当然,我编辑了问题。非常感谢!愚蠢的错误,我感谢你的努力非常感谢!愚蠢的错误,我感谢你的努力