- 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;
|
||||
}
|
||||
}
|
||||
|
48
src/Repository/FavorisRepository.php
Normal file
48
src/Repository/FavorisRepository.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Favoris;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Favoris>
|
||||
*
|
||||
* @method Favoris|null find($id, $lockMode = null, $lockVersion = null)
|
||||
* @method Favoris|null findOneBy(array $criteria, array $orderBy = null)
|
||||
* @method Favoris[] findAll()
|
||||
* @method Favoris[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
|
||||
*/
|
||||
class FavorisRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Favoris::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Favoris[] Returns an array of Favoris objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('f')
|
||||
// ->andWhere('f.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('f.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Favoris
|
||||
// {
|
||||
// return $this->createQueryBuilder('f')
|
||||
// ->andWhere('f.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
Reference in New Issue
Block a user