I am creating a library management system in Symfony to progress.
So I have a book, user, category entity.
So I want to create the borrow a book function.
However, when I perform the action, the book does not change in the database (book must become borrowed).
So here is my controller (knowing that it is a user who borrows, and the user can addBook as there is a join):
<?php
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Book;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
class BookController extends AbstractController
{
//Home page of Book, to render all the books
#[Route('/book', name: 'app_book')]
public function index(ManagerRegistry $doctrine): Response
{
$books = $doctrine->getRepository(Book::class)->findAll();
return $this->render('book/index.html.twig', [
'controller_name' => 'BookController',
'books' => $books,
]);
}
// Show a book
#[Route("/book/{id}", name: 'book_show')]
public function show(Book $article, EntityManagerInterface $manager , Request $request, $id){
$repo = $manager->getRepository(Book::class);
$book = $repo->find($id);
return $this->render('book/show.html.twig', [
'book' => $book,
]);
}
// Display the borrow form
#[Route("/book/borrow/{id}", name: 'borrow_book')]
public function RenderBorrow(Book $book, EntityManagerInterface $manager, $id){
$repo = $manager->getRepository(Book::class);
$book = $repo->find($id);
return $this->render('book/borrow.html.twig', [
'book' => $book,
]);
}
// method to actually borrow the book, then redirect to the book page
#[Route("/book/borrowed/{id}", name: 'book_borrowed')]
public function borrow(Book $book, EntityManagerInterface $manager, $id){
$repo = $manager->getRepository(Book::class);
$book = $repo->find($id);
// return true or false if book is borrowed or not
$availabilty = $book->isBorrowed();
// if available, then the user can borrow it.
if($availabilty){
// we get the user.
/** @var \App\Entity\User $user */
$user = $this->getUser();
$user->addBook($book);
// the book borrow becomes borrowed.
$book->setBorrowed(true);
$manager->persist($book);
$manager->flush();
$manager->persist($user);
$manager->flush();
}
return $this->redirectToRoute('book_show', ['id' => $book->getId()]);
}
}
And my twig borrow.html.twig
{% extends 'base2.html.twig' %}
{% block title %} Emprunter {{ book.title}} {% endblock %}
{% block body %}
<!-- Page to borrow a specified book
-->
<!-- If book is borrowed, then we display yes, or else no. If no, we can display a
button to borrow it -->
<section class="articles">
<form action="{{ path('book_borrowed', {'id' : book.id })}}" method="post">
{% if not book.borrowed %}
<h5> Emprunter </h5>
<h6> Nom du livre : {{ book.title}}</h6>
<h6> Catégorie du livre : {{ book.categorie}}</h6>
<button type="submit" class="btn btn-primary"> Emprunter </button>
</form>
{% else %}
<div class="alert alert-dismissible alert-danger">
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
<strong>Oh !</strong> <a href="#" class="alert-link">Vous tentez d'emprunter un livre déjà emprunté !
</div>
{% endif %}
</section>
{% endblock %}
Please help me, because normally afterwards the book should update and become borrowed...
My Answer:
It looks like you are correctly updating the book entity in your `borrow` method in the controller. However, there are a few things you can check to troubleshoot why the database is not updating:
1. Check if the `setBorrowed` method in your `Book` entity is correctly implemented and updating the `borrowed` property.
2. Make sure that the `isBorrowed` method in your `Book` entity is correctly checking the status of the book.
3. Check if there are any validation constraints in your `Book` entity that might be preventing the update.
4. Verify that the `persist` and `flush` methods are being called in the correct order in your controller method.
5. Check if there are any database transactions that might be preventing the changes from being persisted.
6. You can also try to debug by adding some `var_dump` or `dump` statements in your controller method to see the values of the book entity before and after the update.
By checking these points, you should be able to identify the issue and fix the problem with updating the book entity in the database.
Rate this post
4 of 5 based on 3794 votesComments