Home > OS >  Symfony Crud Form
Symfony Crud Form

Time:08-05

I created a CRUD which manages events, another other CRUD which manages styles of music.

I made a manytomany relationship between the two, and I want to choose from my event creation form my style of music.

So I made a formtype for the events that I use for the creation and for the edition.

The crud worked very well with me doing my relationship. The creation of event still works but I have this error when I want to access the view of the modification form of my event :

Unable to transform value for property path "music_type": Expected a Doctrine\Common\Collections\Collection object.

what do you think ?

part music style for my eventtype :

            ->add('music_type', EntityType::class, [
                'class' => MusicStyle::class,
                'query_builder' => function (MusicStyleRepository $r) {
                    return $r->createQueryBuilder('i')
                        ->orderBy('i.name', 'ASC');
                },
                'label' => 'Style de musique',
                'label_attr' => [
                    'class' => 'form-label mt-4'
                ],
                'choice_label' => 'name',
                'multiple' => true,
                'expanded' => true,
            ])

My entity Event :

     * @return Collection<int, MusicStyle>
     */
    public function getMusicStyles(): Collection
    {
        return $this->musicStyles;
    }

    public function addMusicStyle(MusicStyle $musicStyle): self
    {
        if (!$this->musicStyles->contains($musicStyle)) {
            $this->musicStyles[] = $musicStyle;
            $musicStyle->addEvent($this);
        }

        return $this;
    }

    public function removeMusicStyle(MusicStyle $musicStyle): self
    {
        if ($this->musicStyles->removeElement($musicStyle)) {
            $musicStyle->removeEvent($this);
        }

        return $this;
    }

My eventController


    public function edit(
        EventRepository $repository, 
        Event $event, 
        Request $request, 
        EntityManagerInterface $manager
    ): Response {
        $form = $this->createForm(EventType::class, $event);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            $event = $form->getData();

            $manager->persist($event);
            $manager->flush();

            $this->addFlash(
                'success',
                'Ton event a bien été modifié !'
            );
            
            return $this->redirectToRoute('event.index');
        }
        return $this->render('pages/event/edit.html.twig', [
            'form' => $form->createView()
        ]);    
    }

this may seem like a silly question so i'm novice in symfony

CodePudding user response:

Try changing the property type from Collection to ArrayCollection instead. Because the querybuilder returns an array, not a Collection.

Another thing to check is, if you are initializing the property in your entity constructor, like

public function __construct()
{
    $this->musicStyles = new ArrayCollection();
}
  • Related