0

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()
        ]);    
    }

my migration for the event table

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE event (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT NOT NULL, picture VARCHAR(255) NOT NULL, city VARCHAR(255) NOT NULL, adress VARCHAR(255) NOT NULL, place_name VARCHAR(255) DEFAULT NULL, date DATETIME NOT NULL, music_type VARCHAR(255) NOT NULL, event_type VARCHAR(255) NOT NULL, participants INT DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('CREATE TABLE messenger_messages (id BIGINT AUTO_INCREMENT NOT NULL, body LONGTEXT NOT NULL, headers LONGTEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL, available_at DATETIME NOT NULL, delivered_at DATETIME DEFAULT NULL, INDEX IDX_75EA56E0FB7336F0 (queue_name), INDEX IDX_75EA56E0E3BD61CE (available_at), INDEX IDX_75EA56E016BA31DB (delivered_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
    }

my migration for the ManyToMany

    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE music_style (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('CREATE TABLE music_style_event (music_style_id INT NOT NULL, event_id INT NOT NULL, INDEX IDX_5F0B2D4F7DDE3C52 (music_style_id), INDEX IDX_5F0B2D4F71F7E88B (event_id), PRIMARY KEY(music_style_id, event_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('ALTER TABLE music_style_event ADD CONSTRAINT FK_5F0B2D4F7DDE3C52 FOREIGN KEY (music_style_id) REFERENCES music_style (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE music_style_event ADD CONSTRAINT FK_5F0B2D4F71F7E88B FOREIGN KEY (event_id) REFERENCES event (id) ON DELETE CASCADE');
    }

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

Swanito
  • 17
  • 7
  • Could it be that you are using `->add('music_type', ...)` when it should be `->add('musicStyles', ...)`? – Julien B. Aug 03 '22 at 16:51
  • No the champ in my db is a good name – Swanito Aug 03 '22 at 17:49
  • But the property seems to be $musicStyles – Julien B. Aug 04 '22 at 01:22
  • It's because Doctrine's getResult() method (used under the hood in 'query_builder' option) always returns an array, not Collection object. More details [here](https://stackoverflow.com/questions/8196112/how-to-get-a-collection-in-doctrine2s-query-results). – Vasiliy Fateev Aug 04 '22 at 06:00

1 Answers1

0

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();
}
alexktz
  • 48
  • 6
  • That was it thank you alexktz, but now I get this in my database, and not the music type I expected, how can I do? ```Doctrine\Common\Collections\ArrayCollection@00000000217457f8000000002bd67afb``` – Swanito Aug 04 '22 at 21:45
  • @SwanMarinCarvalheiro How does you DB design look? Have you created it via `bin/console make:migration`? for a many-to-many relation you would have a musicstyle_event table or similiar, where the both IDs of event and music_style are stored – alexktz Aug 05 '22 at 13:35
  • Yes i have this table, I just put you my shemas in the post @alexktz – Swanito Aug 05 '22 at 17:12
  • 1
    Thanks for the update with the db-schema, this helps understanding your problem way better. So you have a `music_type` field in your `event` table, which is also related in the form type. On the other hand you have a many-to-many table for the `event` <-> `music_style` relation. So I guess music_style and music_type are the same thing here? If so, you can rename music_type to musicStyle in your form definition and remove the `musicType` field from the event entity as this is a duplicate to the relation `music_style`. – alexktz Aug 08 '22 at 07:32
  • Perfect @alexktz that was exactly it, thank's you for your help ! – Swanito Aug 24 '22 at 20:57