Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème pour remove son propre compte Symfony

    18 mars 2023 à 19:43:59

    Bonjour.

    je fais un exercice en symphony 6 et php8.

    Je voudrais qu'un utilisateur connecté puisse supprimer son propre compte avec les annonces associées avec ce controller:

        #[Route('/utilisateur/suppression/{id}', name: 'user.delete', methods: ['GET', 'POST'])]
        public function deleteUser(User $user, ManagerRegistry $doctrine): Response
        {
            if (!$this->getUser()) {
                $this->addFlash('danger', 'Impossible de supprimer ce compte !');
                return $this->redirectToRoute('app_home');
            }
            $this->container->get('security.token_storage')->setToken(null);
            $entityManager = $doctrine->getManager();
            $entityManager->remove($user);
            $entityManager->flush();
            return $this->redirectToRoute('app_home');
        }

    Mais malheureusement lors de l'exécution j'ai droit à cette erreur:

    Warning: Undefined array key "user"

    Voici mes entity utilisées

    User.php

    <?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\PasswordAuthenticatedUserInterface;
    use Symfony\Component\Security\Core\User\UserInterface;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    
    
    #[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
    #[ORM\Entity(repositoryClass: UserRepository::class)]
    #[ORM\EntityListeners(['App\EntityListener\UserListener'])]
    class User implements UserInterface, PasswordAuthenticatedUserInterface
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private ?int $id = null;
    
        #[ORM\Column(length: 180, unique: true)]
        #[Assert\Email]
        #[Assert\Length(min:5,max:180)]
        private ?string $email = null;
    
        #[ORM\Column]
        private array $roles = [];
    
        private ?string $plainPassword = null;
    
        /**
         * @var string The hashed password
         */
        #[ORM\Column]
        #[Assert\NotBlank()]
        private ?string $password = 'password';
    
        #[ORM\Column(length: 50, nullable: true)]
        private ?string $name = null;
    
        #[ORM\Column(length: 50, nullable: true)]
        private ?string $firstName = null;
    
        #[ORM\Column(length: 255, nullable: true)]
        private ?string $address = null;
    
        #[ORM\Column(length: 255, nullable: true)]
        private $cv = null;
    
        #[ORM\Column]
        private ?\DateTimeImmutable $createdAt = null;
    
        #[ORM\Column]
        private ?bool $isValid = null;
    
        #[ORM\Column(nullable: true)]
        private ?int $idConsultantValidate = null;
    
        #[ORM\OneToMany(mappedBy: 'user', targetEntity: Annonce::class, orphanRemoval: true)]
        private Collection $recruteur;
    
        public function __construct()
        {
            $this->createdAt = new \DateTimeImmutable();
            $this->isValid = false;
            $this->recruteur = new ArrayCollection();
        }
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getEmail(): ?string
        {
            return $this->email;
        }
    
        public function setEmail(string $email): self
        {
            $this->email = $email;
    
            return $this;
        }
    
        /**
         * A visual identifier that represents this user.
         *
         * @see UserInterface
         */
        public function getUserIdentifier(): string
        {
            return (string) $this->email;
        }
    
        /**
         * @see UserInterface
         */
        public function getRoles(): array
        {
            $roles = $this->roles;
            // guarantee every user at least has ROLE_USER
            $roles[] = 'ROLE_USER';
    
            return array_unique($roles);
        }
    
        public function setRoles(array $roles): self
        {
            $this->roles = $roles;
    
            return $this;
        }
    
        /**
         * Get the value of plainPassword
         */
        public function getPlainPassword()
        {
            return $this->plainPassword;
        }
    
        /**
         * Set the value of plainPassword
         *
         * @return  self
         */
        public function setPlainPassword($plainPassword)
        {
            $this->plainPassword = $plainPassword;
    
            return $this;
        }
    
        /**
         * @see PasswordAuthenticatedUserInterface
         */
        public function getPassword(): string
        {
            return $this->password;
        }
    
        public function setPassword(string $password): self
        {
            $this->password = $password;
    
            return $this;
        }
    
        /**
         * @see UserInterface
         */
        public function eraseCredentials()
        {
            // If you store any temporary, sensitive data on the user, clear it here
            // $this->plainPassword = null;
        }
    
        public function getName(): ?string
        {
            return $this->name;
        }
    
        public function setName(?string $name): self
        {
            $this->name = $name;
    
            return $this;
        }
    
        public function getFirstName(): ?string
        {
            return $this->firstName;
        }
    
        public function setFirstName(?string $firstName): self
        {
            $this->firstName = $firstName;
    
            return $this;
        }
    
        public function getAddress(): ?string
        {
            return $this->address;
        }
    
        public function setAddress(?string $address): self
        {
            $this->address = $address;
    
            return $this;
        }
    
        public function getCv()
        {
            return $this->cv;
        }
    
        public function setCv($cv)
        {
            $this->cv = $cv;
    
            return $this;
        }
    
        public function getCreatedAt(): ?\DateTimeImmutable
        {
            return $this->createdAt;
        }
    
        public function setCreatedAt(\DateTimeImmutable $createdAt): self
        {
            $this->createdAt = $createdAt;
    
            return $this;
        }
    
        public function getIsValid(): ?bool
        {
            return $this->isValid;
        }
    
        public function setIsValid(bool $isValid): self
        {
            $this->isValid = $isValid;
    
            return $this;
        }
    
        public function getIdConsultantValidate(): ?int
        {
            return $this->idConsultantValidate;
        }
    
        public function setIdConsultantValidate(?int $idConsultantValidate): self
        {
            $this->idConsultantValidate = $idConsultantValidate;
    
            return $this;
        }
    
        /**
         * @return Collection<int, Annonce>
         */
        public function getIdRecruteurCreate(): Collection
        {
            return $this->recruteur;
        }
    
        public function addRecruteur(Annonce $recruteur): self
        {
            if (!$this->recruteur->contains($recruteur)) {
                $this->recruteur->add($recruteur);
                $recruteur->setRecruteur($this);
            }
    
            return $this;
        }
    
        public function removeIdRecruteurCreate(Annonce $recruteur): self
        {
            if ($this->recruteur->removeElement($recruteur)) {
                // set the owning side to null (unless already changed)
                if ($recruteur->getRecruteur() === $this) {
                    $recruteur->setRecruteur(null);
                }
            }
    
            return $this;
        }
    }
    



    Annonce.php

    <?php
    
    namespace App\Entity;
    
    use App\Entity\User;
    use App\Repository\AnnonceRepository;
    use Doctrine\ORM\Mapping as ORM;
    
    #[ORM\Entity(repositoryClass: AnnonceRepository::class)]
    class Annonce
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private ?int $id = null;
    
        #[ORM\Column(length: 255)]
        private ?string $poste = null;
    
        #[ORM\Column(length: 255)]
        private ?string $lieu_de_travail = null;
    
        #[ORM\Column(length: 255)]
        private ?string $description = null;
    
        #[ORM\Column]
        private ?\DateTimeImmutable $createAt = null;
    
        #[ORM\Column(length: 255, nullable: true)]
        private ?string $id_candidat_valid = null;
    
        #[ORM\Column(length: 255, nullable: true)]
        private ?string $id_candidat_invalid = null;
    
    
        #[ORM\Column(length: 255, nullable: true)]
        private ?string $id_validaton_consultant = null;
    
        #[ORM\Column]
        private ?bool $isValid = null;
    
        #[ORM\ManyToOne(inversedBy: 'recruteur')]
        #[ORM\JoinColumn(nullable: false)]
        private ?User $recruteur = null;
    
        public function __construct()
        {
            $this->createAt = new \DateTimeImmutable();
            $this->isValid = false;
        }
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getPoste(): ?string
        {
            return $this->poste;
        }
    
        public function setPoste(string $poste): self
        {
            $this->poste = $poste;
    
            return $this;
        }
    
        public function getLieuDeTravail(): ?string
        {
            return $this->lieu_de_travail;
        }
    
        public function setLieuDeTravail(string $lieu_de_travail): self
        {
            $this->lieu_de_travail = $lieu_de_travail;
    
            return $this;
        }
    
        public function getDescription(): ?string
        {
            return $this->description;
        }
    
        public function setDescription(string $description): self
        {
            $this->description = $description;
    
            return $this;
        }
    
        public function getCreateAt(): ?\DateTimeImmutable
        {
            return $this->createAt;
        }
    
        public function setCreateAt(\DateTimeImmutable $createAt): self
        {
            $this->createAt = $createAt;
    
            return $this;
        }
    
        public function getIdCandidatValid(): ?string
        {
            return $this->id_candidat_valid;
        }
    
        public function setIdCandidatValid(?string $id_candidat_valid): self
        {
            $this->id_candidat_valid = $id_candidat_valid;
    
            return $this;
        }
    
        public function getIdCandidatInvalid(): ?string
        {
            return $this->id_candidat_invalid;
        }
    
        public function setIdCandidatInvalid(?string $id_candidat_invalid): self
        {
            $this->id_candidat_invalid = $id_candidat_invalid;
    
            return $this;
        }
    
        public function getIdValidatonConsultant(): ?string
        {
            return $this->id_validaton_consultant;
        }
    
        public function setIdValidatonConsultant(string $id_validaton_consultant): self
        {
            $this->id_validaton_consultant = $id_validaton_consultant;
    
            return $this;
        }
    
                /**
         * Get the value of isValid
         */ 
        public function getIsValid()
        {
            return $this->isValid;
        }
    
        /**
         * Set the value of isValid
         *
         * @return  self
         */ 
        public function setIsValid($isValid)
        {
            $this->isValid = $isValid;
    
            return $this;
        }
    
        public function getRecruteur(): ?User
        {
            return $this->recruteur;
        }
    
        public function setRecruteur(?User $recruteur): self
        {
            $this->recruteur = $recruteur;
    
            return $this;
        }
    }
    



    J'aimerais bien un coup de pouce ou des pistes SVP.

    Merci.

    • Partager sur Facebook
    • Partager sur Twitter
      18 mars 2023 à 22:39:48

      Salut,

      Plutôt mappedBy="recruteur" ? Il faut bien nommer tes propriétés..  Comment tu génère tes entités ? Il faut utiliser le pluriel pour tes collections et il faut penser en termes d'objets lors de ta conception

      • Partager sur Facebook
      • Partager sur Twitter
      le bienfait n'est jamais perdu
        19 mars 2023 à 8:34:05

        Bonjour,

        L'énoncé de l'erreur est pourtant explicite. Tu utilises la clé nommée user pour lire un tableau alors que cette clé n'existe pas pour ce tableau. Normalement, tu dois même avoir l'endroit du code où se situe cette erreur dans le message d'erreur.

        A toi

        • Partager sur Facebook
        • Partager sur Twitter
          19 mars 2023 à 13:26:33

          @CarréDas1 , souvent un mauvais mapping génère ce genre d'erreur.
          • Partager sur Facebook
          • Partager sur Twitter
          le bienfait n'est jamais perdu
            19 mars 2023 à 13:39:50

            WillyKouassi a écrit:

            Salut,

            Plutôt mappedBy="recruteur" ? Il faut bien nommer tes propriétés..  Comment tu génère tes entités ? Il faut utiliser le pluriel pour tes collections et il faut penser en termes d'objets lors de ta conception

            Merci WillyKouassi et CarréDas1 ! C'était bien cela.

            -
            Edité par lanfyp 19 mars 2023 à 13:40:14

            • Partager sur Facebook
            • Partager sur Twitter

            Problème pour remove son propre compte Symfony

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            • Editeur
            • Markdown