<?php
namespace App\Entity;
use App\Repository\BasketRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: BasketRepository::class)]
class Basket
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToOne(inversedBy: 'basket', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\ManyToMany(targetEntity: BookAd::class, inversedBy: 'baskets')]
private Collection $books;
public function __construct()
{
$this->books = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection<int, BookAd>
*/
public function getBooks(): Collection
{
return $this->books;
}
public function addBook(BookAd $book): self
{
if (!$this->books->contains($book)) {
$this->books->add($book);
}
return $this;
}
public function removeBook(BookAd $book): self
{
$this->books->removeElement($book);
return $this;
}
// calcul du prix total du panier de l'utilisateur
// d'après la correction donnée le 06/04
// j'aurais pu trouver ça tout seul mais je me voyais pas utiliser des fonctions en twig
public function getTotal(): float
{
$total = 0;
foreach ($this->books as $book)
$total += $book->getPrice();
return $total;
}
}