Php Symfony 5中EntityType的数据覆盖问题(与ChoiceType的问题相同)

Php Symfony 5中EntityType的数据覆盖问题(与ChoiceType的问题相同),php,symfony,Php,Symfony,这就是我关心的问题。我有一个“子”实体,其中包含$services和$users,并链接了许多 我的应用程序的目标是能够将一个子项分配给我的服务的用户。 因此,根据我所使用的服务,我不会有相同的用户选择来分配 在我的ChildType中的“users”字段中,我添加了一个键“query\u builder”,它将只检索相关的用户 My ChildType.php <?php namespace App\Form; use App\Entity\Child; use App\Entity

这就是我关心的问题。我有一个“子”实体,其中包含$services和$users,并链接了许多

我的应用程序的目标是能够将一个子项分配给我的服务的用户。 因此,根据我所使用的服务,我不会有相同的用户选择来分配

在我的ChildType中的“users”字段中,我添加了一个键“query\u builder”,它将只检索相关的用户

My ChildType.php

<?php

namespace App\Form;

use App\Entity\Child;
use App\Entity\Service;
use App\Entity\Sexe;
use App\Entity\User;
use App\Repository\ServiceRepository;
use App\Repository\UserRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;

class ChildType extends AbstractType
{
    /** @var User $user */
    private $user;

    private $serviceRepository;

    private $authorization;


    public function __construct(Security $security, AuthorizationCheckerInterface $authorization, ServiceRepository $serviceRepository)
    {
        $this->user = $security->getUser();
        $this->authorization = $authorization;
        $this->serviceRepository = $serviceRepository;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /** @var Service $service */
        $service = $this->user->getService();
        $builder
            ->add('nom', TextType::class)
            ->add('prenom', TextType::class)
            ->add('sexe', EntityType::class, [
                'class' => Sexe::class,
            ])
            ->add('dateDeNaissance', DateType::class, [
                'widget' => 'single_text'
            ])
            ->add('lieuDeNaissance', TextType::class)
            ->add('nationalite', TextType::class)
            ->add('dateElaborationPpe', DateType::class, [
                'widget' => 'single_text'
            ]);

        if ($this->authorization->isGranted('ROLE_ADMIN')) {
            $builder->add('users', EntityType::class, [
                'class' => User::class,
                'label' => 'Affecter à un utilisateur',
                'query_builder' => function (UserRepository $userRepository) use ($service) {
                    return $userRepository->findByCurrentService($service);
                },
                'multiple' => true,
                'expanded' => true,
            ]);

            $builder->add('services', EntityType::class, [
                'class' => Service::class,
                'label' => 'Affecter à un service',
                'choices' => $this->serviceRepository->findByDepartement($service->getDepartement()),
                'choice_attr' => function($key, $val, $index) use ($service) {
                    return([]);
                    return $key->getCategorie()->getId() === $service->getCategorie()->getId() ? 
                    ['disabled' => 'disabled'] : 
                    [];
                },
                'multiple' => true,
                'expanded' => true,
            ]);
        }
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Child::class,
        ]);
    }
}

User.php:

<?php

namespace App\Entity;

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 App\Entity\Service;
use App\Traits\BlameableEntity;
use App\Traits\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 *
 */
class User implements UserInterface, \Serializable
{
    use BlameableEntity;
    use TimestampableEntity;
    
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    private $fullName;

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     * @Assert\NotBlank()
     * @Assert\Length(min=2, max=50)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     * @Assert\Email()
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    private $password;

    /**
     * @var array
     *
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var \DateTime
     * 
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $lastLogin;
    
    /**
     * @var \DateTime
     * 
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $secondToLastLogin;

    /**
     * @ORM\ManyToOne(targetEntity="Service", inversedBy="users")
     */
    private $service;

    /**
     * @ORM\ManyToMany(targetEntity="Child", mappedBy="users")
     */
    private $children;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFullName(): ?string
    {
        return $this->fullName;
    }

    public function setFullName(string $fullName): self
    {
        $this->fullName = $fullName;
        return $this;
    }

    public function getUsername(): ?string
    {
        return $this->username;
    }

    public function setUsername(string $username): self
    {
        $this->username = $username;
        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;
        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;
        return $this;
    }

    /**
     * Returns the roles or permissions granted to the user for security.
     */
    public function getRoles(): array
    {
        $roles = $this->roles;

        // guarantees that a user always has at least one role for security
        if (empty($roles)) {
            $roles[] = 'ROLE_USER';
        }

        return array_unique($roles);
    }

    public function setRoles(array $roles): void
    {
        $this->roles = $roles;
    }

     /**
     * Get the value of lastLogin
     *
     * @return  \DateTime
     */ 
    public function getLastLogin()
    {
        return $this->lastLogin;
    }

    /**
     * Set the value of lastLogin
     *
     * @param  \DateTime  $lastLogin
     *
     * @return  self
     */ 
    public function setLastLogin(\DateTime $lastLogin)
    {
        $this->lastLogin = $lastLogin;

        return $this;
    }

    /**
     * Get the value of secondToLastLogin
     *
     * @return  \DateTime
     */ 
    public function getSecondToLastLogin()
    {
        return $this->secondToLastLogin;
    }

    /**
     * Set the value of secondToLastLogin
     *
     * @param  \DateTime  $secondToLastLogin
     *
     * @return  self
     */ 
    public function setSecondToLastLogin(\DateTime $secondToLastLogin)
    {
        $this->secondToLastLogin = $secondToLastLogin;

        return $this;
    }

    /**
     *
     * {@inheritdoc}
     */
    public function getSalt(): ?string
    {
        return null;
    }

    /**
     * Removes sensitive data from the user.
     *
     * {@inheritdoc}
     */
    public function eraseCredentials(): void
    {
        // if you had a plainPassword property, you'd nullify it here
        // $this->plainPassword = null;
    }

    /**
     * {@inheritdoc}
     */
    public function serialize(): string
    {
        // add $this->salt too if you don't use Bcrypt or Argon2i
        return serialize([$this->id, $this->username, $this->password]);
    }

    /**
     * {@inheritdoc}
     */
    public function unserialize($serialized): void
    {
        // add $this->salt too if you don't use Bcrypt or Argon2i
        [$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
    }

    /**
     * Get the value of service
     */ 
    public function getService()
    {
        return $this->service;
    }

    /**
     * Set the value of service
     *
     * @return  self
     */ 
    public function setService($service)
    {
        $this->service = $service;

        return $this;
    }

    public function hasService(): bool
    {
        return !empty($this->getService());
    }

    /**
     * @return Collection|Child[]
     */
    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function addChild(Child $child): self
    {
        if (!$this->children->contains($child)) {
            $this->children[] = $child;
            $child->addUser($this);
        }

        return $this;
    }

    public function removeChild(Child $child): self
    {
        if ($this->children->removeElement($child)) {
            $child->removeUser($this);
        }

        return $this;
    }

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


它很可能是实体设置与表单的组合。显示您的实体,为简洁起见,删除除关系及其setter、getter、adder、remover之外的所有内容。您好@Jakumi,谢谢您的帮助。我编辑了我的帖子以添加实体。另一方面,我有一个关于User.php实体的addUser方法的问题。事实上,当我更新我的用户时,不会调用此方法(在调试器xDebug中可以看到)。目前我正在兜圈子。再次感谢。没有被调用的addUser可能与返回集合的getter相关,并且在相应的表单字段中没有将byReference设置为false(在这些情况下,表单可能直接添加到集合中)
<?php

namespace App\Entity;

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 App\Entity\Service;
use App\Traits\BlameableEntity;
use App\Traits\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 *
 */
class User implements UserInterface, \Serializable
{
    use BlameableEntity;
    use TimestampableEntity;
    
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    private $fullName;

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     * @Assert\NotBlank()
     * @Assert\Length(min=2, max=50)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     * @Assert\Email()
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    private $password;

    /**
     * @var array
     *
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var \DateTime
     * 
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $lastLogin;
    
    /**
     * @var \DateTime
     * 
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $secondToLastLogin;

    /**
     * @ORM\ManyToOne(targetEntity="Service", inversedBy="users")
     */
    private $service;

    /**
     * @ORM\ManyToMany(targetEntity="Child", mappedBy="users")
     */
    private $children;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFullName(): ?string
    {
        return $this->fullName;
    }

    public function setFullName(string $fullName): self
    {
        $this->fullName = $fullName;
        return $this;
    }

    public function getUsername(): ?string
    {
        return $this->username;
    }

    public function setUsername(string $username): self
    {
        $this->username = $username;
        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;
        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;
        return $this;
    }

    /**
     * Returns the roles or permissions granted to the user for security.
     */
    public function getRoles(): array
    {
        $roles = $this->roles;

        // guarantees that a user always has at least one role for security
        if (empty($roles)) {
            $roles[] = 'ROLE_USER';
        }

        return array_unique($roles);
    }

    public function setRoles(array $roles): void
    {
        $this->roles = $roles;
    }

     /**
     * Get the value of lastLogin
     *
     * @return  \DateTime
     */ 
    public function getLastLogin()
    {
        return $this->lastLogin;
    }

    /**
     * Set the value of lastLogin
     *
     * @param  \DateTime  $lastLogin
     *
     * @return  self
     */ 
    public function setLastLogin(\DateTime $lastLogin)
    {
        $this->lastLogin = $lastLogin;

        return $this;
    }

    /**
     * Get the value of secondToLastLogin
     *
     * @return  \DateTime
     */ 
    public function getSecondToLastLogin()
    {
        return $this->secondToLastLogin;
    }

    /**
     * Set the value of secondToLastLogin
     *
     * @param  \DateTime  $secondToLastLogin
     *
     * @return  self
     */ 
    public function setSecondToLastLogin(\DateTime $secondToLastLogin)
    {
        $this->secondToLastLogin = $secondToLastLogin;

        return $this;
    }

    /**
     *
     * {@inheritdoc}
     */
    public function getSalt(): ?string
    {
        return null;
    }

    /**
     * Removes sensitive data from the user.
     *
     * {@inheritdoc}
     */
    public function eraseCredentials(): void
    {
        // if you had a plainPassword property, you'd nullify it here
        // $this->plainPassword = null;
    }

    /**
     * {@inheritdoc}
     */
    public function serialize(): string
    {
        // add $this->salt too if you don't use Bcrypt or Argon2i
        return serialize([$this->id, $this->username, $this->password]);
    }

    /**
     * {@inheritdoc}
     */
    public function unserialize($serialized): void
    {
        // add $this->salt too if you don't use Bcrypt or Argon2i
        [$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
    }

    /**
     * Get the value of service
     */ 
    public function getService()
    {
        return $this->service;
    }

    /**
     * Set the value of service
     *
     * @return  self
     */ 
    public function setService($service)
    {
        $this->service = $service;

        return $this;
    }

    public function hasService(): bool
    {
        return !empty($this->getService());
    }

    /**
     * @return Collection|Child[]
     */
    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function addChild(Child $child): self
    {
        if (!$this->children->contains($child)) {
            $this->children[] = $child;
            $child->addUser($this);
        }

        return $this;
    }

    public function removeChild(Child $child): self
    {
        if ($this->children->removeElement($child)) {
            $child->removeUser($this);
        }

        return $this;
    }

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

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Repository\ServiceRepository;
use App\Entity\Categorie;
use App\Entity\Departement;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

/**
 * @ORM\Entity(repositoryClass=ServiceRepository::class)
 */
class Service
{
    public const ASE = 'ASE';
    public const MDPH = 'MDPH';
    public const E_N = 'Éducation Nationale';

    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     * 
     * @ORM\Column(type="string")
     */
    private $nom;

    /**
     * @var Departement
     * 
     * @ORM\ManyToOne(targetEntity="Departement")
     * @ORM\JoinColumn(nullable=false)
     */
    private $departement;

    /**
     * @var Categorie
     * 
     * @ORM\ManyToOne(targetEntity="Categorie")
     * @ORM\JoinColumn(nullable=false)
     */
    private $categorie;

    /**
     * @ORM\OneToMany(targetEntity="User", mappedBy="service")
     */
    private $users;

    /**
     * @ORM\ManyToMany(targetEntity="Child", mappedBy="services")
     */
    private $children;

    /**
     * @ORM\ManyToMany(targetEntity="Element", mappedBy="services")
     */
    private $elements;

    public function __construct()
    {
        $this->users = new ArrayCollection();
        $this->children = new ArrayCollection();
        $this->elements = new ArrayCollection();
    }

    /**
     * Get the value of id
     *
     * @return  int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of id
     *
     * @param  int  $id
     *
     * @return  self
     */
    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of nom
     *
     * @return  string
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set the value of nom
     *
     * @param  string  $nom
     *
     * @return  self
     */
    public function setNom(string $nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get the value of departement
     *
     * @return  Departement
     */
    public function getDepartement()
    {
        return $this->departement;
    }

    /**
     * Set the value of departement
     *
     * @param  Departement  $departement
     *
     * @return  self
     */
    public function setDepartement(Departement $departement)
    {
        $this->departement = $departement;

        return $this;
    }

    /**
     * Get the value of categorie
     *
     * @return  Categorie
     */
    public function getCategorie()
    {
        return $this->categorie;
    }

    /**
     * Set the value of categorie
     *
     * @param  Categorie  $categorie
     *
     * @return  self
     */
    public function setCategorie(Categorie $categorie)
    {
        $this->categorie = $categorie;

        return $this;
    }

    /**
     * @return Collection|Users[]
     */ 
    public function getUsers(): Collection
    {
        return $this->users;
    }

    public function addUser(User $user): self
    {
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
            $user->setService($this);
        }

        return $this;
    }

    public function removeUser(User $user): self
    {
        if ($this->users->removeElement($user)) {
            // set the owning side to null (unless already changed)
            if ($user->getService() === $this) {
                $user->setService(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|Children[]
     */ 
    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function addChild(Child $child): self
    {
        if (!$this->children->contains($child)) {
            $this->children[] = $child;
            $child->addService($this);
        }

        return $this;
    }

    public function removeChild(Child $child): self
    {
        if ($this->children->removeElement($child)) {
            $child->removeService($this);
        }

        return $this;
    }

    /**
     * @return Collection|Elements[]
     */ 
    public function getElements(): Collection
    {
        return $this->elements;
    }

    public function addElements(Element $element): self
    {
        if (!$this->children->contains($element)) {
            $this->elements[] = $element;
            $element->addService($this);
        }

        return $this;
    }

    public function removeElement(Element $element): self
    {
        if ($this->elements->removeElement($element)) {
            $element->removeService($this);
        }

        return $this;
    }

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

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Entity\Service;
use App\Entity\Sexe;
use App\Entity\User;
use DateTime;
use App\Repository\ChildRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Traits\TimestampableEntity;
use App\Traits\BlameableEntity;

/**
 * @ORM\Entity(repositoryClass=ChildRepository::class)
 */
class Child
{
    use BlameableEntity;
    use TimestampableEntity;

    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    private $nom;

    /**
     * @var string
     * 
     * @ORM\Column(type="string")
     */
    private $prenom;


    /**
     * @var Sexe
     * 
     * @ORM\ManyToOne(targetEntity="Sexe")
     */
    private $sexe;

    /**
     * @var date
     * 
     * @ORM\Column(type="date")
     */
    private $dateDeNaissance;

    /**
     * @var string
     * 
     * @ORM\Column(type="string")
     */
    private $lieuDeNaissance;

    /**
     * @var string
     * 
     * @ORM\Column(type="string")
     */
    private $nationalite;

    /**
     * @var date
     * 
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $dateElaborationPpe;


    /**
     * @var Service[]|Collection
     * 
     * @ORM\ManyToMany(targetEntity="Service", inversedBy="children")
     */
    private $services;

    /**
     * @var User[]|Collection
     * 
     * @ORM\ManyToMany(targetEntity="User", inversedBy="children")
     */
    private $users;

    /**
     * @var Element[]|Collection
     * 
     * @ORM\OneToMany(targetEntity="Element", mappedBy="child")
     */
    private $elements;

    public function __construct()
    {
        $this->services = new ArrayCollection();
        $this->users = new ArrayCollection();
        $this->elements = new ArrayCollection();
    }

    /**
     * Get the value of id
     *
     * @return  int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of id
     *
     * @param  int  $id
     *
     * @return  self
     */
    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return string
     */
    public function getNumDossier()
    {
        return str_pad($this->id, 6, "0", STR_PAD_LEFT);
    }

    /**
     * Get the value of nom
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set the value of nom
     *
     * @return  self
     */
    public function setNom(string $nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get the value of prenom
     */
    public function getPrenom()
    {
        return $this->prenom;
    }

    /**
     * Set the value of prenom
     *
     * @return  self
     */
    public function setPrenom(string $prenom)
    {
        $this->prenom = $prenom;

        return $this;
    }

    /**
     * Get the value of sexe
     *
     * @return  Sexe
     */
    public function getSexe()
    {
        return $this->sexe;
    }

    /**
     * Set the value of sexe
     *
     * @param  Sexe  $sexe
     *
     * @return  self
     */
    public function setSexe(Sexe $sexe)
    {
        $this->sexe = $sexe;

        return $this;
    }

    /**
     * Get the value of dateDeNaissance
     */
    public function getDateDeNaissance()
    {
        return $this->dateDeNaissance;
    }

    /**
     * Set the value of dateDeNaissance
     *
     * @return  self
     */
    public function setDateDeNaissance(datetime $dateDeNaissance)
    {
        $this->dateDeNaissance = $dateDeNaissance;

        return $this;
    }

    /**
     * Get the value of lieuDeNaissance
     */
    public function getLieuDeNaissance()
    {
        return $this->lieuDeNaissance;
    }

    /**
     * Set the value of lieuDeNaissance
     *
     * @return  self
     */
    public function setLieuDeNaissance(string $lieuDeNaissance)
    {
        $this->lieuDeNaissance = $lieuDeNaissance;

        return $this;
    }

    /**
     * Get the value of nationalite
     */
    public function getNationalite()
    {
        return $this->nationalite;
    }

    /**
     * Set the value of nationalite
     *
     * @return  self
     */
    public function setNationalite(string $nationalite)
    {
        $this->nationalite = $nationalite;

        return $this;
    }

    /**
     * Get the value of dateElaborationPpe
     */
    public function getDateElaborationPpe()
    {
        return $this->dateElaborationPpe;
    }

    /**
     * Set the value of dateElaborationPpe
     *
     * @return  self
     */
    public function setDateElaborationPpe(datetime $dateElaborationPpe)
    {
        $this->dateElaborationPpe = $dateElaborationPpe;

        return $this;
    }

    /**
     * @return Collection|Service[]
     */
    public function getServices(): Collection
    {
        return $this->services;
    }

    public function addService(Service $service): self
    {
        if (!$this->services->contains($service)) {
            $this->services[] = $service;
        }

        return $this;
    }

    public function removeService(Service $service): self
    {
        $this->services->removeElement($service);

        return $this;
    }

    /**
     * @return Collection|User[]
     */
    public function getUsers(): Collection
    {
        return $this->users;
    }

    public function addUser(User $user): self
    {
        $test = 'test';
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
        }

        return $this;
    }

    public function removeUser(User $user): self
    {
        $this->users->removeElement($user);

        return $this;
    }

    public function getNbUsers()
    {
        return count($this->getUsers());
    }

    /**
     * @return Collection|Element[]
     */
    public function getElements(): Collection
    {
        return $this->elements;
    }

    public function addElement(Element $element): self
    {
        if (!$this->elements->contains($element)) {
            $this->elements[] = $element;
        }

        return $this;
    }

    public function removeElement(Element $element): self
    {
        $this->elements->removeElement($element);

        return $this;
    }


    public function isNotAssigned(): bool
    {
        return (!$this->getNbUsers());
    }

    public function __toString(): string
    {
        return $this->getNom() . ' ' . $this->getPrenom();
    }
}