src/Entity/Basket.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BasketRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassBasketRepository::class)]
  8. class Basket
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column]
  13.     private ?int $id null;
  14.     #[ORM\OneToOne(inversedBy'basket'cascade: ['persist''remove'])]
  15.     #[ORM\JoinColumn(nullablefalse)]
  16.     private ?User $user null;
  17.     #[ORM\ManyToMany(targetEntityBookAd::class, inversedBy'baskets')]
  18.     private Collection $books;
  19.     public function __construct()
  20.     {
  21.         $this->books = new ArrayCollection();
  22.     }
  23.     public function getId(): ?int
  24.     {
  25.         return $this->id;
  26.     }
  27.     public function getUser(): ?User
  28.     {
  29.         return $this->user;
  30.     }
  31.     public function setUser(User $user): self
  32.     {
  33.         $this->user $user;
  34.         return $this;
  35.     }
  36.     /**
  37.      * @return Collection<int, BookAd>
  38.      */
  39.     public function getBooks(): Collection
  40.     {
  41.         return $this->books;
  42.     }
  43.     public function addBook(BookAd $book): self
  44.     {
  45.         if (!$this->books->contains($book)) {
  46.             $this->books->add($book);
  47.         }
  48.         return $this;
  49.     }
  50.     public function removeBook(BookAd $book): self
  51.     {
  52.         $this->books->removeElement($book);
  53.         return $this;
  54.     }
  55.     // calcul du prix total du panier de l'utilisateur
  56.     // d'après la correction donnée le 06/04
  57.     // j'aurais pu trouver ça tout seul mais je me voyais pas utiliser des fonctions en twig
  58.     public function getTotal(): float
  59.     {
  60.         $total 0;
  61.         foreach ($this->books as $book)
  62.             $total += $book->getPrice();
  63.         return $total;
  64.     }
  65. }