Bonjour, j'ai cette erreur avec Symfony depuis hier soir, j'ai tenté avec chatGTP et différentes recherches sur internet mais je n'ai pas réussi à trouver la solution. Pourriez-vous m'aider ?
En faite je souhaite afficher la commande du client dans son profil, j'ai fait une relation avec mon entité Commande et User en ManyToOne.... Rien de fou fou... J'ai codé pour récupérer la commande et l'afficher par la vue dans le profil du client mais j'ai cette erreur...
https://i.postimg.cc/nzj3rcxL/temp-Imagezh1t-Re.jpg
Typed property App\Entity\User::$commandes must not be accessed before initialization
Auriez-vous une idée svp ?
<?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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(length: 255)]
private ?string $prenom = null;
#[ORM\Column(length: 255)]
private ?string $nom = null;
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;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): 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;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
/**
* @ORM\OneToMany(targetEntity=Commande::class, mappedBy="user")
*/
private Collection $commandes;
public function __construct()
{
$this->commandes = new ArrayCollection();
}
/**
* @return Collection<Commande>
*/
public function getCommandes(): Collection
{
return $this->commandes;
}
public function addCommande(Commande $commande): self
{
if (!$this->commandes->contains($commande)) {
$this->commandes[] = $commande;
$commande->setUser($this);
}
return $this;
}
public function removeCommande(Commande $commande): self
{
if ($this->commandes->removeElement($commande)) {
// set the owning side to null (unless already changed)
if ($commande->getUser() === $this) {
$commande->setUser(null);
}
}
return $this;
}
}
<?php
namespace App\Entity;
use App\Repository\CommandeRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CommandeRepository::class)]
class Commande
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $date = null;
#[ORM\Column(nullable: true)]
private ?int $montantTotal = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $etat = null;
#[ORM\Column(nullable: true)]
private ?int $numeroCommande = null;
#[ORM\ManyToOne(inversedBy: 'commandes')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
public function getId(): ?int
{
return $this->id;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(?\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getMontantTotal(): ?int
{
return $this->montantTotal;
}
public function setMontantTotal(?int $montantTotal): self
{
$this->montantTotal = $montantTotal;
return $this;
}
public function getEtat(): ?string
{
return $this->etat;
}
public function setEtat(?string $etat): self
{
$this->etat = $etat;
return $this;
}
public function getNumeroCommande(): ?int
{
return $this->numeroCommande;
}
public function setNumeroCommande(?int $numeroCommande): self
{
$this->numeroCommande = $numeroCommande;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}
<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProfileController extends AbstractController
{
#[Route('/profile', name: 'app_profile')]
public function index(): Response
{
// Récupérer l'utilisateur actuellement connecté
$user = $this->getUser();
if (!$user instanceof User) {
// Gérer le cas où l'utilisateur n'est pas connecté ou n'est pas trouvé
throw $this->createNotFoundException('Utilisateur non trouvé');
}
// Récupérer les commandes de l'utilisateur
$commandes = $user->getCommandes();
// Passez l'objet $user et les commandes à la vue
return $this->render('profile/index.html.twig', [
'user' => $user,
'commandes' => $commandes,
]);
}
}
{% extends 'base.html.twig' %}
{% block title %}Mon profil{% endblock %}
{% block body %}
<div class="soustitre">
<h2>Mon compte</h2>
</div>
<div class="connexioncompte">
Bonjour, {{ user.getPrenom() }} !
<a href="{{ path('app_logout') }}">Se déconnecter</a>
</div>
<div class="commandes-utilisateur">
<h3>Mes commandes</h3>
{% if user.getCommandes().isEmpty() %}
<p>Aucune commande pour le moment.</p>
{% else %}
<ul>
{% for commande in user.getCommandes() %}
<li>
Commande n° {{ commande.getId() }} effectuée le {{ commande.getDate()|date('d/m/Y H:i') }}
<ul>
{% for produit in commande.produits %} {# Assurez-vous que votre relation s'appelle "produits" #}
<li>{{ produit.getTitre() }} ({{ produit.getPrix() }} €)</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
{% endif %}
</div>
<div class="current-time">
Heure actuelle : {{ "now"|date('d/m/Y H:i') }}
</div>
{% endblock %}
Bonjour, j'ai cette erreur avec Symfony depuis hier soir, j'ai tenté avec chatGTP et différentes recherches sur internet mais je n'ai pas réussi à trouver la solution. Pourriez-vous m'aider ?
En faite je souhaite afficher la commande du client dans son profil, j'ai fait une relation avec mon entité Commande et User en ManyToOne.... Rien de fou fou... J'ai codé pour récupérer la commande et l'afficher par la vue dans le profil du client mais j'ai cette erreur...
https://i.postimg.cc/nzj3rcxL/temp-Imagezh1t-Re.jpg
Typed property App\Entity\User::$commandes must not be accessed before initialization
Auriez-vous une idée svp ?
[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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(length: 255)]
private ?string $prenom = null;
#[ORM\Column(length: 255)]
private ?string $nom = null;
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;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): 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;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
/**
* @ORM\OneToMany(targetEntity=Commande::class, mappedBy="user")
*/
private Collection $commandes;
public function __construct()
{
$this->commandes = new ArrayCollection();
}
/**
* @return Collection<Commande>
*/
public function getCommandes(): Collection
{
return $this->commandes;
}
public function addCommande(Commande $commande): self
{
if (!$this->commandes->contains($commande)) {
$this->commandes[] = $commande;
$commande->setUser($this);
}
return $this;
}
public function removeCommande(Commande $commande): self
{
if ($this->commandes->removeElement($commande)) {
// set the owning side to null (unless already changed)
if ($commande->getUser() === $this) {
$commande->setUser(null);
}
}
return $this;
}
}
[/PHP]
[PHP]
<?php
namespace App\Entity;
use App\Repository\CommandeRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CommandeRepository::class)]
class Commande
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $date = null;
#[ORM\Column(nullable: true)]
private ?int $montantTotal = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $etat = null;
#[ORM\Column(nullable: true)]
private ?int $numeroCommande = null;
#[ORM\ManyToOne(inversedBy: 'commandes')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
public function getId(): ?int
{
return $this->id;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(?\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getMontantTotal(): ?int
{
return $this->montantTotal;
}
public function setMontantTotal(?int $montantTotal): self
{
$this->montantTotal = $montantTotal;
return $this;
}
public function getEtat(): ?string
{
return $this->etat;
}
public function setEtat(?string $etat): self
{
$this->etat = $etat;
return $this;
}
public function getNumeroCommande(): ?int
{
return $this->numeroCommande;
}
public function setNumeroCommande(?int $numeroCommande): self
{
$this->numeroCommande = $numeroCommande;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}
[/PHP]
[PHP]
<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProfileController extends AbstractController
{
#[Route('/profile', name: 'app_profile')]
public function index(): Response
{
// Récupérer l'utilisateur actuellement connecté
$user = $this->getUser();
if (!$user instanceof User) {
// Gérer le cas où l'utilisateur n'est pas connecté ou n'est pas trouvé
throw $this->createNotFoundException('Utilisateur non trouvé');
}
// Récupérer les commandes de l'utilisateur
$commandes = $user->getCommandes();
// Passez l'objet $user et les commandes à la vue
return $this->render('profile/index.html.twig', [
'user' => $user,
'commandes' => $commandes,
]);
}
}
[/PHP]
[html]
{% extends 'base.html.twig' %}
{% block title %}Mon profil{% endblock %}
{% block body %}
<div class="soustitre">
<h2>Mon compte</h2>
</div>
<div class="connexioncompte">
Bonjour, {{ user.getPrenom() }} !
<a href="{{ path('app_logout') }}">Se déconnecter</a>
</div>
<div class="commandes-utilisateur">
<h3>Mes commandes</h3>
{% if user.getCommandes().isEmpty() %}
<p>Aucune commande pour le moment.</p>
{% else %}
<ul>
{% for commande in user.getCommandes() %}
<li>
Commande n° {{ commande.getId() }} effectuée le {{ commande.getDate()|date('d/m/Y H:i') }}
<ul>
{% for produit in commande.produits %} {# Assurez-vous que votre relation s'appelle "produits" #}
<li>{{ produit.getTitre() }} ({{ produit.getPrix() }} €)</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
{% endif %}
</div>
<div class="current-time">
Heure actuelle : {{ "now"|date('d/m/Y H:i') }}
</div>
{% endblock %}
[/html]