Bonjour a tous,
Je suis entrain de coder un formulaire pour modifier son mot de passe sur symfony seulement voilà, quand je getUser, impossible d'accéder aux méthodes de mon entité et je n'arrive pas à comprendre pourquoi.
Voici mon code:
Controller:
/**
* @Route("/profil/edit", name="user_profile_edit")
*/
public function editProfile(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $encoder)
{
$user = $this->getUser();
$editForm = $this->createForm(EditType::class, $user);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()){
$hashed = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($hashed);
$em->persist($user);
$em->flush();
$this->addFlash('message', 'Profil mis à jour');
return $this->redirectToRoute('user');
}
return $this->render('user/edit.html.twig', [
"editForm"=> $editForm->createView()
]);
}
Mon entité:
<?php
namespace App\Entity;
use App\Repository\ParticipantsRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=ParticipantsRepository::class)
*/
class Participants implements UserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", nullable=false)
*/
private $no_participant;
/**
* @ORM\Column(type="string", length=30, nullable=false, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=30, nullable=false)
*/
private $nom;
/**
* @ORM\Column(type="string", length=30, nullable=false)
*/
private $prenom;
/**
* @ORM\Column(type="string", length=15)
*/
private $telephone;
/**
* @ORM\Column(type="string", length=20, nullable=false, unique=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $password;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $actif = '1';
/**
* @ORM\Column(type="integer", nullable=false)
*/
private $campus_no_campus = '0';
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @return mixed
*/
public function getNoParticipant()
{
return $this->no_participant;
}
/**
* @param mixed $no_participant
*/
public function setNoParticipant($no_participant): void
{
$this->no_participant = $no_participant;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
/**
* @param mixed $username
*/
public function setUsername($username): void
{
$this->username = $username;
}
/**
* @return mixed
*/
public function getNom()
{
return $this->nom;
}
/**
* @param mixed $nom
*/
public function setNom($nom): void
{
$this->nom = $nom;
}
/**
* @return mixed
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* @param mixed $prenom
*/
public function setPrenom($prenom): void
{
$this->prenom = $prenom;
}
/**
* @return mixed
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* @param mixed $telephone
*/
public function setTelephone($telephone): void
{
$this->telephone = $telephone;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email): void
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password): void
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getActif()
{
return $this->actif;
}
/**
* @param mixed $actif
*/
public function setActif($actif): void
{
$this->actif = $actif;
}
/**
* @return mixed
*/
public function getCampusNoCampus()
{
return $this->campus_no_campus;
}
/**
* @param mixed $campus_no_campus
*/
public function setCampusNoCampus($campus_no_campus): void
{
$this->campus_no_campus = $campus_no_campus;
}
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* @param mixed $roles
*/
public function setRoles($roles): void
{
$this->roles = $roles;
}
public function getSalt(){return null;}
public function eraseCredentials(){}
}
De ce que j'ai compris je ne peux accéder à mes méthodes d'entité que si je créé un nouveau User et pas si je le get, comment faire du coup pour hériter des bonnes méthodes ?
Trouvé ! pour ceux qui auraient besoin, il manquait$user = $editForm->getData();
dans le traitement du formulaire pour pouvoir hériter des méthodes de l'utilisateur