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!
One problem is that you made address a public property. In Doctrine 2 you need to make your properties either private or protected. D2 relies on this to implement lazy loading. Basically, your address is never getting loaded though why you are getting an array is a bit of a puzzle. Are you initializing address to be an array in your constructor? Maybe a copy/paste error?
You should have:
class User extends BaseUser
{
protected $address;
public function getAddress() { return $this->address; }
You will also need to ensure that a UserObject always has an Address object or the form will complain.
========================================================
Looking at your pastebin, I see:
class User extends BaseUser
{
public function __construct()
{
$this->adresse = new \Doctrine\Common\Collections\ArrayCollection();
}
Which not only explains the unwanted array but will also mess up the fos user base class since it's constructor is not called.
I would suggest that you strip your form types down to just the minimum user/address and get things working. Then add in your villia and all the other stuff. In fact, just start with creating a simple user and then add in address.
You didn't show how you were creating the user object but remember that it is up to you to ensure that an adress object exists before the form kicks off. Doctrine will not create it for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With