0
votes

i need select username of entity " user "on my form, but i get this fail:

Entity of type "Doctrine\ORM\PersistentCollection" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?

i try add public function __toString() on my entity user, but dont work

form

class AsignarEventoFormType extends AbstractType{
    
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('users', EntityType::class, [
            'class' => User::class,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.nombre', 'ASC');
            },
            'choice_label' => 'nombre',
        ]);
    }
    
}

entity of user :

<?php

declare(strict_types=1);
 
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\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\PersistentCollection;
 

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
 
    /**
     * @ORM\Column(name="nombre", type="string", length=255, nullable=false)
     */
    private $nombre;
    /**
     * @ORM\Column(name="apellido", type="string", length=255, nullable=false)
     */
    private $apellido;
    
    /**
     * @var string|null
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     * @Assert\NotBlank
     * @Assert\Email(
     *      message = "El email '{{ value }}' no es valido"
     *      
     * )
     */
    private $email;
    
    /**
     * @ORM\Column(name="password", type="string", length=255, nullable=false)
     */
    private $password;
    
    /**
     * @ORM\Column(name="role", type="string", length=255, nullable=false)
     */
    private $role;


    /**
     * @var \DateTime|null
     *
     * @ORM\Column(name="fecha", type="datetime", nullable=true)
     */
    private $fecha;    
    
    /**
     * @ORM\Column(name="foto", type="string", length=255, nullable=false)
     */
    private $foto;    
    /**
     * @ORM\ManyToMany(targetEntity="Evento", inversedBy="users", cascade={"persist"})
     * @ORM\JoinTable(
     *     name="user_evento",
     *     joinColumns={
     *          @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     *     },
     *     inverseJoinColumns={
     *          @ORM\JoinColumn(name="evento_id", referencedColumnName="id")
     *     }
     * )
     */
    private $eventos;


    /**
     * @ORM\ManyToMany(targetEntity="Sala", inversedBy="users", cascade={"persist"})
     * @ORM\JoinTable(
     *     name="user_sala",
     *     joinColumns={
     *          @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     *     },
     *     inverseJoinColumns={
     *          @ORM\JoinColumn(name="sala_id", referencedColumnName="id")
     *     }
     * )
     */
    private $salas;    
 
    public function __construct()
    {
        $this->eventos = new ArrayCollection();
        $this->salas = new ArrayCollection();

    }
 
    public function getId(): int
    {
        return $this->id;
    }
 
    public function setNombre(string $nombre): self
    {
        $this->nombre = $nombre;
 
        return $this;
    }
 
    public function getNombre(): string
    {
        return $this->nombre;
    }
 
    public function setApellido(string $apellido): self
    {
        $this->apellido = $apellido;
 
        return $this;
    }
 
    public function getApellido(): string
    {
        return $this->apellido;
    }


    public function setEmail(string $email): self
    {
        $this->email = $email;
 
        return $this;
    }
 
    public function getEmail(): string
    {
        return $this->email;
    }
    
    
    public function setPassword(string $password): self
    {
        $this->password = $password;
 
        return $this;
    }
 
    public function getPassword(): string
    {
        return $this->password;
    }
    
    public function setRole(string $role): self
    {
        $this->role = $role;
 
        return $this;
    }
 
    public function getRole(): string
    {
        return $this->role;
    }    
    public function setFoto(string $foto): self
    {
        $this->foto = $foto;
 
        return $this;
    }
 
    public function getFoto(): string
    {
        return $this->foto;

    }   



    public function getFecha()
    {
        return $this->fecha;
    }

    public function setFecha($fecha): self
    {
        $this->fecha = $fecha;

        return $this;
    }    
    public function addEvento(Evento $evento): self
    {
        $this->eventos[] = $evento;
 
        return $this;
    }
 
    public function removeEvento(Evento $evento): bool
    {
        return $this->eventos->removeElement($evento);
    }
 
    public function getEventos(): Collection
    {
        return $this->eventos;
    }


    public function addSala(Sala $sala): self
    {
        $this->salas[] = $salas;
 
        return $this;
    }
 
    public function removeSala(Sala $sala): bool
    {
        return $this->salas->removeElement($sala);
    }
 
    public function getSalas(): Collection
    {
        return $this->salas;
    }    

    public function getUsername(){
        return $this->email;
    }

public function getSalt(){
        return null;
    }

public function getRoles(){
        return array('ROLE_USER');
    }

public function eraseCredentials(){}

public function __toString() {
    return $this->id;
 }
}

capture 1 capture2

1
I've reproduced your situation in my Symfony, and have no problem. Maybe, it's one of your relations causes problem? Can you provide full stacktrace of an error?nikserg
@nikserg thanks for responde, i add images of the error on my post, thankssELE
try to remove __constructor from your entity class.nikserg
@nikserg i remove the construct, and i get the same error :(ELE

1 Answers

0
votes

You should add multiple => true to your users form field options.