Hi all I'am having some issues with the Symfony2 FormBuilder, actually, i have an entity user who is linked (OneByOne) to an entity Adress, it seems to be really simple but when i'm trying to embed the AddressType Form into the UserType One i'm facing this exception :
The form's view data is expected to be an instance of class Acme\Bundle\AddressBundle\Entity\Adresse, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of Acme\Bundle\AddressBundle\Entity\Adresse
I put here some code (reduced to be readable) to make my problem more understable :
My User class (which extends the FosUserBundle one's) :
class User extends BaseUser
{
...
/**
* @ORM\OneToOne(targetEntity="Acme\bundle\AddressBundle\Entity\Address", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true)
* @Assert\Valid
*/
public $address;
.......
}
The linked Form Type buildForm function :
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
// add your custom field
$builder->add('name','text')
->add('address',new AddressType(),array(
'data_class' => 'Acme\Bundle\AddressBundle\Entity\Address'
)
);
}
My Address Form Type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('city','text')
->add('title','text');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Bundle\AddressBundle\Entity\Address'
));
}
Thank you in advance for your help!