- 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

50
src/Entity/Favoris.php Normal file
View 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;
}
}

View File

@ -3,6 +3,8 @@
namespace App\Entity; namespace App\Entity;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
@ -34,6 +36,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(type: "json")] #[ORM\Column(type: "json")]
private array $roles = []; private array $roles = [];
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Favoris::class)]
private Collection $favoris;
public function __construct()
{
$this->favoris = new ArrayCollection();
}
public function getId(): ?int 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 $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;
}
} }

View 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()
// ;
// }
}

View File

@ -4,7 +4,6 @@
{% block body %} {% block body %}
<div> <div>
<div class="w-1/2 py-2 flex flex-col justify-center mt-5 mx-auto"> <div class="w-1/2 py-2 flex flex-col justify-center mt-5 mx-auto">
<h1 class="text-2xl font-bold">Recherche de livre pour : {{ query }}</h1> <h1 class="text-2xl font-bold">Recherche de livre pour : {{ query }}</h1>
@ -41,7 +40,11 @@
</div> </div>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
class="absolute top-2 right-2 w-6 h-6 cursor-pointer"> class="absolute top-2 right-2 w-6 h-6 cursor-pointer transition-colors duration-300"
x-data="{ liked: false }"
:fill="liked ? 'red' : 'none'"
:stroke="liked ? 'red' : 'currentColor'"
@click="liked = !liked">
<path stroke-linecap="round" stroke-linejoin="round" <path stroke-linecap="round" stroke-linejoin="round"
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"/> d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"/>
</svg> </svg>

View File

@ -23,5 +23,7 @@
{% endblock %} {% endblock %}
{% block body %}{% endblock %} {% block body %}{% endblock %}
</div> </div>
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
</body> </body>
</html> </html>