S1arl
S1arl

Reputation: 55

Embed Form and EntityType multiple

I have embed form, inside embed form there is an EntityType with multiple=> true. When i am saving to databse i get this: Doctrine\Common\Collections\ArrayCollection@000000...

When i change EntityType to CHoiceType with multiple=> true everything seems to be correct.

/**
 * Set emotions
 *
 * @param simple_array $emotions
 *
 * @return Movies
 */
public function setEmotions($emotions) {
$this->emotions = $emotions;

return $this;
}

FormType

->add('emotions', EntityType::class, array(
'class' => 'MovieBundle:Emotions',
'multiple' => true, 
'choice_label' => 'emotion',
))

I have no idea where the problem is. Is it something with embed form, or i need data transformer?

Upvotes: 1

Views: 783

Answers (1)

Anas EL KORCHI
Anas EL KORCHI

Reputation: 2048

Whene you set multiple to true symfony set your attribute value into an array, if you set multiple to false this attribute will be set like it is so the difference between them is :

  • multiple: true gonna call a setter in your class this setter should have an array argument like setEmotions(ArrayCollection $emotions), and it will be persisted one by one using a loop.
  • multiple : false gonna call a setter in your class with an argument of type Emotion like setEmotions(Emotion $emotion) and it will be persisted only the object that he get from getEmotions() in this case he will persist only the DoctrineArrayCollection not the objects wrapped in it and this is the reason of your exception

Upvotes: 1

Related Questions