简体   繁体   中英

Adding an entity in an other bundle through forms symfony2

I wanted to ask about adding an entity in one bundle through a form of an entity in an other different bundle.

I feel this is impossible in symfony because when I tried it, I got a warning.

Catchable Fatal Error: Argument 1 passed to xxx\twoBundle\Entity\entity1::setUser() must be an instance of xxx\oneBundle\Entity\User, array given, called in C:\wamp\www\Symfony\vendor\symfony\symfony\src\Symfony\Component\Form\Util\PropertyPath.php on line 538 and defined in C:\wamp\www\Symfony\src\xxx\twoBundle\Entity\entity1.php line 446 

Any ideas???

Ok create a form type for your entity and set the dataClass:

class MySecondEntityFormType {

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

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'XXX\TwoBundle\Entity\MySecondEntity',
    ));
}

Now in the main form add the newly created form:

public function buildForm(FormBuilderInterface $builder, array $options)
{        
    ...
    $builder->add('mySecondEntity', new MySecondEntityFormType());
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'compound' => true
    ));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM