У меня есть две сущности: User и UserDetails.Эти сущности имеют непосредственное отношение.Я хочу отредактировать User и User Details в одной форме, но почему-то я не могу заставить это работать.
У меня следующая ошибка:
Expected argument of type "array or (\Traversable and \ArrayAccess)", "App\Entity\UserDetails" given
Это мой код
Сущность пользователя:
/**
* @ORM\Table(name="Users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
public $username;
/**
* @ORM\Column(type="string", length=64)
*/
public $password;
/**
* @ORM\Column(type="string", length=128, unique=true)
* @Assert\Email()
*/
public $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
public $isActive;
/**
* @ORM\Column(name="roles", type="array")
*/
public $roles = [];
/**
* @ORM\OneToOne(targetEntity="App\Entity\UserDetails", mappedBy="user", cascade={"persist", "remove"})
*/
private $userDetails;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Assignment", mappedBy="user")
*/
private $assignments;
public function __construct()
{
$this->isActive = true;
$this->assignments = new ArrayCollection();
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
/**
* @param mixed $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getSalt()
{
// The bcrypt and argon2i algorithms don't require a separate salt.
// You *may* need a real salt if you choose a different encoder.
return null;
}
public function getRoles()
{
$roles = $this->roles;
if (!in_array('ROLE_USER', $roles)) {
$roles[] = 'ROLE_USER';
}
return $roles;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
public function setRoles(array $roles)
{
$this->roles = $roles;
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->isActive
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->isActive
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
public function getUserDetails(): ?UserDetails
{
return $this->userDetails;
}
public function setUserDetails(UserDetails $userDetails): self
{
$this->userDetails = $userDetails;
// set the owning side of the relation if necessary
if ($this !== $userDetails->getUser()) {
$userDetails->setUser($this);
}
return $this;
}
/**
* @return Collection|Assignment[]
*/
public function getAssignments(): Collection
{
return $this->assignments;
}
public function addAssignment(Assignment $assignment): self
{
if (!$this->assignments->contains($assignment)) {
$this->assignments[] = $assignment;
$assignment->addUser($this);
}
return $this;
}
public function removeAssignment(Assignment $assignment): self
{
if ($this->assignments->contains($assignment)) {
$this->assignments->removeElement($assignment);
$assignment->removeUser($this);
}
return $this;
}
}
Сущность UserDetails:
/**
* @ORM\Entity(repositoryClass="App\Repository\UserDetailsRepository")
*/
class UserDetails
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
public $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $street;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $street_number;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $zipcode;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $birthplace;
/**
* @ORM\Column(type="date", nullable=true)
*/
public $birthdate;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $city;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $surname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $gender;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $mobile_number;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $function;
/**
* @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="userDetails", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
public $user;
/**
* @return mixed
*/
public function getFunction()
{
return $this->function;
}
/**
* @param mixed $function
*/
public function setFunction($function)
{
$this->function = $function;
}
public function getId()
{
return $this->id;
}
public function getStreet(): ?string
{
return $this->street;
}
public function setStreet(?string $street): self
{
$this->street = $street;
return $this;
}
public function getStreetNumber(): ?string
{
return $this->street_number;
}
public function setStreetNumber(?string $street_number): self
{
$this->street_number = $street_number;
return $this;
}
public function getZipcode(): ?string
{
return $this->zipcode;
}
public function setZipcode(?string $zipcode): self
{
$this->zipcode = $zipcode;
return $this;
}
public function getBirthplace(): ?string
{
return $this->birthplace;
}
public function setBirthplace(?string $birthplace): self
{
$this->birthplace = $birthplace;
return $this;
}
public function getBirthdate(): ?\DateTimeInterface
{
return $this->birthdate;
}
public function setBirthdate(?\DateTimeInterface $birthdate): self
{
$this->birthdate = $birthdate;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(?string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(?string $gender): self
{
$this->gender = $gender;
return $this;
}
public function getMobileNumber(): ?string
{
return $this->mobile_number;
}
public function setMobileNumber(?string $mobile_number): self
{
$this->mobile_number = $mobile_number;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
}
Метод, вызывающий ошибку:
/**
* @Route("/users/edit/{id}", name="editUsers")
* @IsGranted("ROLE_ADMIN")
*/
public function edit(Request $request, User $user, UserPasswordEncoderInterface $passwordEncoder){
$form = $this->createForm(UserFormType::class, $user);
$form->add('userDetails', CollectionType::class, array(
'entry_type' => UserDetailsFormType::class,
'entry_options' => array('label' => false),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success', 'User updated!');
/* @var User $updated_user */
$updated_user = $form->getData();
if($updated_user->getPassword() != null) {
$password = $passwordEncoder->encodePassword($updated_user, $updated_user->getPassword());
$updated_user->setPassword($password);
}
$em = $this->getDoctrine()->getManager();
$em->persist($updated_user);
$em->flush();
return $this->redirectToRoute('users');
}
return $this->render('user/edit.html.twig', array(
'form' => $form->createView(),
'user' => $user
));
}
Я не знаю, является ли это правильным способом одновременного редактирования UserDetails и UserData.Я открыт для другого предложения о том, как это сделать!Я надеюсь, что кто-то может помочь мне решить эту проблему!