- Ajout de la table Favoris.
- Relation entre user et favoris en onetomany.
This commit is contained in:
50
src/Entity/Favoris.php
Normal file
50
src/Entity/Favoris.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\FavorisRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: FavorisRepository::class)]
|
||||
class Favoris
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $id_google = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'favoris')]
|
||||
private ?User $user = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getIdGoogle(): ?string
|
||||
{
|
||||
return $this->id_google;
|
||||
}
|
||||
|
||||
public function setIdGoogle(string $id_google): static
|
||||
{
|
||||
$this->id_google = $id_google;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUser(): ?User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function setUser(?User $user): static
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user