- Ajout de la table Favoris.

- Relation entre user et favoris en onetomany.
This commit is contained in:
2025-01-29 15:18:01 +01:00
parent 74288b17a9
commit c20f3bc933
5 changed files with 145 additions and 2 deletions

View File

@ -3,6 +3,8 @@
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\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
@ -33,6 +35,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(type: "json")]
private array $roles = [];
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Favoris::class)]
private Collection $favoris;
public function __construct()
{
$this->favoris = new ArrayCollection();
}
public function getId(): ?int
@ -129,4 +139,34 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this->email; // Ou $this->pseudo si tu préfères utiliser le pseudo
}
/**
* @return Collection<int, Favoris>
*/
public function getFavoris(): Collection
{
return $this->favoris;
}
public function addFavori(Favoris $favori): static
{
if (!$this->favoris->contains($favori)) {
$this->favoris->add($favori);
$favori->setUser($this);
}
return $this;
}
public function removeFavori(Favoris $favori): static
{
if ($this->favoris->removeElement($favori)) {
// set the owning side to null (unless already changed)
if ($favori->getUser() === $this) {
$favori->setUser(null);
}
}
return $this;
}
}