I’ve a Boat class for entity properties, new boat is added by a form (BoatAdType), here’s my function of my controller :
#[Route('/new-ad', name: 'app_new_ad')]
public function index(Request $request, EntityManagerInterface $entityManager): Response
{
$boatAd = new Boat();
$form = $this->createForm(BoatAdType::class, $boatAd);
Clock::set(new MockClock());
$clock = Clock::get();
$user = $this->getUser();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$boatAd = $form->getData();
$boatAd->setDateCreation($clock->now());
$boatAd->setFKBoatUser($user);
$entityManager->persist($boatAd);
$entityManager->flush();
// $posted = $this->generateUrl('app_ad_post_success'); not the most important subject, I comment it because it doesn't work
return $this->redirectToRoute('app_ad_post_success');
}
return $this->render('pages/boat_ad_form/index.html.twig', [
'form' => $form,
]);
}
I want to reset AUTO_INCREMENT after deleting rows in a table to avoid a gap between id values.
For that, I found this SQL query here :
ALTER TABLE sales_data AUTO_INCREMENT=1;
I also see this command (answer is for postgreSQL but I can use it for me table in MySQL, I didn’t know which is better, if there is one) :
TRUNCATE TABLE table_name RESTART IDENTITY;
I saw we can create function inside BoatRepository to operate this query, like symfony docs says it. I tried to write one :
public function startFollowingCurrentOrder() {
$conn = $this->getEntityManager()->getConnection();
$sql = '
ALTER TABLE boat AUTO_INCREMENT=1;
';
return $conn->executeQuery($sql);
}
I though import it in my form just before the return stament in index(), it is right ? And is this process correct ?