J. Ciszewski
J. Ciszewski

Reputation: 53

Multiple entity in one form

I have following problem. I would like to create a form which works on two entities. I have followings entities: first entity is user entity, and it is connected relationally to the second entity(column bip relate to entity "BIP"). Now I have following code:

BIPType.php:

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name');
}

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'AppBundle\Entity\Bip',
    );
}

And UserType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $biptype = new BIPType();
    $builder->add('bip', $biptype);
}

public function getParent()
{
    return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    // Or for Symfony < 2.8
    // return 'fos_user_registration';
}

public function getBlockPrefix()
{
    return 'app_user_registration';
}

// For Symfony 2.x
public function getName()
{
    return $this->getBlockPrefix();
}

Unfortunately, error has occured

Catchable Fatal Error: Argument 1 passed to UserBundle\Entity\User::setBip() must be an instance of AppBundle\Entity\Bip, array given, called in /home/spake/php/gryf/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 556 and defined

What to do, my friends? Thanks in advice.

Upvotes: 1

Views: 166

Answers (1)

angrinessfap
angrinessfap

Reputation: 474

You need to declare the Entity class of that Form Class like this:

$builder->add('bip', new BipType(), array(
    'data_class' => 'namespace/to/BipEntity'
));

Upvotes: 1

Related Questions