6

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!

2
  • Have you tried what is suggested in the error message? Commented Jul 26, 2013 at 2:27
  • Yes i've tried it but when i do iti have this message : spl_object_hash() expects parameter 1 to be object, string given Commented Jul 26, 2013 at 15:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Cerad, thanks, i've made changes from public to protected, i'm not intializing adress to be an array in the constructor and the User always has an Address object linked with
Your error message has ...Entity\Adresse... but your entity is called Address. Copy paste problem? Are you sure you are passing a User entity to the form? It's almost like you have two versions of User on your system.
Cerad, it's a copy paste problem, consider adresse as address, i'm working with FOSUserBundle and its RegistrationFormType, the only User entity used is the FOSUSerBundle extended one (Acme/Bundle/UserBundle/User)
Do you have other form types as well? Any chance you have data_class => "Address" by accident in another form type? Consider using pastebin to show your complete classes. I suspect you have a simple error somewhere. You also passing data_class in your builder->add, this is not necessary.
Yes I have 1 other form type, you will find the whole code here : pastebin.com/Hdh5ATZi thanks!
|
0

Your Address is an Entity so you need to let your form know that the field type is loaded from an entity. See here: http://symfony.com/doc/2.2/reference/forms/types/entity.html

Try something like this:

$builder->add('name', 'text')
        ->add('address', 'entity',array(
              'class' => 'AddressBundle:Address',
              'property' => 'address'
));

1 Comment

An entity type is used when you need to pick from a list of existing entities. Not exactly what the op is trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.