PS : j'ai fait un DD $commandes cela m'affiche un tableau vide
Voici quelques extraits de code...
Le Controller Commande
<?php
namespace App\Controller;
use App\Entity\Commande;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
class OrderController extends AbstractController
{
#[Route('/order', name: 'app_order')]
public function index(EntityManagerInterface $entityManager): Response
{
// Récupérer le client connecté (vous devrez ajuster cela en fonction de votre authentification)
$client = $this->getUser();
// Récupérer les commandes du client
$commandes = $entityManager->getRepository(Commande::class)->findBy(['user' => $this->getUser()]);
// dd($commandes);
return $this->render('order/index.html.twig', [
'commandes' => $commandes,
]);
}
}
L'Entity Commande<?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 $quantite = null;
#[ORM\ManyToOne(inversedBy: 'commandes')]
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 getQuantite(): ?int
{
return $this->quantite;
}
public function setQuantite(?int $quantite): self
{
$this->quantite = $quantite;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}
Le Controller User<?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 UsersController extends AbstractController
{
#[Route('/users', name: 'users_list')]
public function index(): Response
{
$userRepository = $this->getDoctrine()->getRepository(User::class);
$users = $userRepository->findAll();
return $this->render('Users/index.html.twig', [
'users' => $users,
]);
}
#[Route('/users/{id}', name: 'users_delete', methods: ['POST'])]
public function delete(User $user): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($user);
$entityManager->flush();
// You can redirect to the users list after deletion or do any other action here.
return $this->redirectToRoute('users_list');
}
}
L'Entity User<?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;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Commande::class)]
private Collection $commandes;
public function __construct()
{
$this->commandes = 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;
}
/**
* @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;
}
/**
* @return Collection<int, Commande>
*/
public function getCommandes(): Collection
{
return $this->commandes;
}
public function addCommande(Commande $commande): self
{
if (!$this->commandes->contains($commande)) {
$this->commandes->add($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;
}
}
Et pour finir ma vue...{% extends 'base.html.twig' %}
{% block title %}Hello OrderController!{% endblock %}
{% block body %}
<div class="soustitre"> <h2>Historique des commandes</h2> </div>
<main class="liste"> </main>
<table>
<thead>
<tr>
<th>Date</th>
<th>Quantité</th>
<th>Client Email</th>
</tr>
</thead>
<tbody>
{% for commande in commandes %}
<tr>
<td>{{ commande.getDate()|date('Y-m-d H:i:s') }}</td>
<td>{{ commande.getQuantite() }}</td>
<td>{{ commande.getUser().getEmail() }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}