I am currently taking a course on OpenClassroom to learn how to develop an API in Symfony.
I am using Symfony 5.4.13 and php 7.4.9
I arrived at the time of the course we must make our API self-discoverable and we must therefore use JMSSerializer.
So I had to change the way I define the groups when I serialize an object.
In this function for example:
/**
* Renvoie les informations de tout les livres
* @Route("/api/books", name="all_book", methods={"GET"})
*/
public function getAllBooks(BookRepository $bookRepository, SerializerInterface $serializer, Request $request, TagAwareCacheInterface $cache): JsonResponse
{
$page = $request->get('page', 1);
$limit = $request->get('limit', 3);
$idCache = "getAllBooks-" . $page . "-" . $limit;
$jsonBookList = $cache->get($idCache, function (ItemInterface $item) use ($bookRepository, $page, $limit, $serializer) {
echo ("L'ELEMENT N'EST PAS ENCORE EN CACHE !\n");
$item->tag("booksCache");
$bookList = $bookRepository->findAllWithPagination($page, $limit);
$context = SerializationContext::create()->setGroups(array('getBooks'));
return $serializer->serialize($bookList, 'json', $context);
});
return new JsonResponse($jsonBookList, Response::HTTP_OK, [], true);
}
My Entity Book.php:
<?php
namespace App\Entity;
use Assert\lenght;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\BookRepository;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Heatoas\Configuration\Annotation as Hatoas;
/**
* @Hateoas\Relation(
* "self",
* href = @Hateoas\Route(
* "detailBook",
* parameters = { "id" = "expr(object.getId())" }
* ),
* exclusion = @Hateoas\Exclusion(groups="getBooks")
* )
*
*/
/**
* @ORM\Entity(repositoryClass=BookRepository::class)
*/
class Book
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups("getBooks", "getAuthors")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups("getBooks", "getAuthors")
* @Assert\NotBlank(message="Le titre du livre est obligatoire")
* @Assert\Length(min=1, max=255, minMessage="Le titre doit faire au moins {{limit}} caractères", maxMessage=" le titre ne doit pas faire plus de {{limit}} caractères")
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups("getBooks", "getAuthors")
*/
private $coverText;
/**
* @ORM\ManyToOne(targetEntity=Author::class, inversedBy="books")
* @ORM\JoinColumn(onDelete="CASCADE")
* @Groups("getBooks")
*
*/
private $author;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getCoverText(): ?string
{
return $this->coverText;
}
public function setCoverText(?string $coverText): self
{
$this->coverText = $coverText;
return $this;
}
public function getAuthor(): ?Author
{
return $this->author;
}
public function setAuthor(?Author $author): self
{
$this->author = $author;
return $this;
}
}
The error :
{
"status": 500,
"message": "Warning: Invalid argument supplied for foreach()"
}
If anyone has a clue, thank you in advance :)