Bonjour,
J'ai une relation Many To Many entre une table Event et Category.
La mise à jour de la table relationnel ne se fait pas.
Dans mon appli j'ai d'autre tables en ManyToMany et d'ordinaire je n'ai pas de problèmes. Mais ici ça veut pas.
Sinon, j'ai bien la table Event qui se met à jour.
EventController
#[Route('/new', name: 'new', methods: ['GET', 'POST'])]
public function new(Request $request, EventRepository $eventRepository): Response
{
$event = new Event();
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$categories = $form->get('categories')->getData();
foreach ($categories as $category) {
$event->addCategory($category);
}
$eventRepository->save($event, true);
$this->addFlash('success', 'L\'événement a bien été créé');
return $this->redirectToRoute('admin_event_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('admin/event/new.html.twig', [
'event' => $event,
'form' => $form,
]);
}
Ma relation sur l'entitée Event avec le addCategory
#[ORM\ManyToMany(targetEntity: Category::class, mappedBy: 'event')]
private Collection $categories;
public function addCategory(Category $category): self
{
if (!$this->categories->contains($category)) {
$this->categories->add($category);
$category->addEvent($this);
}
return $this;
}
Ma relation sur l'entitée Category
#[ORM\ManyToMany(targetEntity: Event::class, inversedBy: 'categories')]
private Collection $event;
Et la add de mon EventType
->add('categories', EntityType::class, [
'label' => 'Catégories',
'class' => Category::class,
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'attr' => [
'placeholder' => 'Catégories',
]
])
J'ai vérifier avec un dd($categories) que le $form->get récupère la bonne valeur, ce qui est le cas.
J'ai aussi testé en remplaçant $eventRepository->save($event, true) par $this->em->flush(), dans ce cas la BDD ne se met pas à jour.
Ce qui le rend fou, c'est qu'il n'y a aucun messages d'erreur.
Sinon, j'ai bien le addFlash et la redirection se fait.
Merci, bonne journée.
Hello ! J'ai trouvé ! Il suffisait d'inverser le coté fort. J'ai donc passé mon entitée Event en inversedBy et Category en mappedBy
Salut,
Est ce que tu as fait un dd($event) après le foreach ? Pour voir si tu as les données.
J'y avais pas pensé.
Avec le dd($event), effectivement, je retrouve la categorie.
EventController.php on line 95:
App\Entity\Event {#622 ▼
-id: 106
-status: 1
-title: "test6"
-date: DateTimeImmutable @1680343200 {#618 ▶}
-address: "22 rue des platanes"
-postalCode: "46465"
-city: "Tourquoin"
-description: "<p>vfdsfdsqfd</p>"
-nbMinus: 15
-nbRegistrant: null
-addedAt: DateTimeImmutable @1680013270 {#615 ▶}
-updatedAt: null
-isActive: 1
-categories: Doctrine\ORM\PersistentCollection {#644 ▶}
-carpool: Doctrine\ORM\PersistentCollection {#632 ▶}
-child: Doctrine\ORM\PersistentCollection {#639 ▶}
}
Et si je fais :
foreach ($categories as $category) {
dd($category);
$event->addCategory($category);
}
J'obtien :
EventController.php on line 93:
App\Entity\Category {#1309 ▼
-id: 2
-name: "U6"
-addedAt: DateTimeImmutable @1678962319 {#1308 ▶}
-updatedAt: null
-isActive: 1
-child: Doctrine\ORM\PersistentCollection {#1310 ▶}
-event: Doctrine\ORM\PersistentCollection {#1312 ▶}
}
Même en regardant dans le log, je ne trouve rien de bizard
[2023-03-29T15:40:40.401671+02:00] doctrine.DEBUG: Executing statement: SELECT e0_.id AS id_0, e0_.status AS status_1, e0_.title AS title_2, e0_.date AS date_3, e0_.address AS address_4, e0_.postal_code AS postal_code_5, e0_.city AS city_6, e0_.nb_minus AS nb_minus_7, e0_.nb_registrant AS nb_registrant_8, e0_.is_active AS is_active_9, c1_.name AS name_10 FROM event e0_ LEFT JOIN category_event c2_ ON e0_.id = c2_.event_id LEFT JOIN category c1_ ON c1_.id = c2_.category_id WHERE e0_.is_active = ? (parameters: array{"1":1}, types: array{"1":5}) {"sql":"SELECT e0_.id AS id_0, e0_.status AS status_1, e0_.title AS title_2, e0_.date AS date_3, e0_.address AS address_4, e0_.postal_code AS postal_code_5, e0_.city AS city_6, e0_.nb_minus AS nb_minus_7, e0_.nb_registrant AS nb_registrant_8, e0_.is_active AS is_active_9, c1_.name AS name_10 FROM event e0_ LEFT JOIN category_event c2_ ON e0_.id = c2_.event_id LEFT JOIN category c1_ ON c1_.id = c2_.category_id WHERE e0_.is_active = ?","params":{"1":1},"types":{"1":5}} []
[2023-03-29T15:40:40.403497+02:00] doctrine.DEBUG: Executing statement: SELECT e0_.id AS id_0, e0_.status AS status_1, e0_.title AS title_2, e0_.date AS date_3, e0_.address AS address_4, e0_.postal_code AS postal_code_5, e0_.city AS city_6, e0_.nb_minus AS nb_minus_7, e0_.nb_registrant AS nb_registrant_8, e0_.is_active AS is_active_9, c1_.name AS name_10 FROM event e0_ LEFT JOIN category_event c2_ ON e0_.id = c2_.event_id LEFT JOIN category c1_ ON c1_.id = c2_.category_id WHERE e0_.is_active = ? (parameters: array{"1":0}, types: array{"1":5}) {"sql":"SELECT e0_.id AS id_0, e0_.status AS status_1, e0_.title AS title_2, e0_.date AS date_3, e0_.address AS address_4, e0_.postal_code AS postal_code_5, e0_.city AS city_6, e0_.nb_minus AS nb_minus_7, e0_.nb_registrant AS nb_registrant_8, e0_.is_active AS is_active_9, c1_.name AS name_10 FROM event e0_ LEFT JOIN category_event c2_ ON e0_.id = c2_.event_id LEFT JOIN category c1_ ON c1_.id = c2_.category_id WHERE e0_.is_active = ?","params":{"1":0},"types":{"1":5}} []
[2023-03-29T15:40:40.415742+02:00] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []
[2023-03-29T15:40:40.424370+02:00] doctrine.INFO: Disconnecting [] []