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