通过表单symfony2在另一个包中添加实体

时间:2013-05-02 09:59:15

标签: php symfony doctrine

我想问一下通过另一个不同包中的实体形式在一个包中添加实体。

我觉得这在symfony中是不可能的,因为当我尝试它时,我收到了警告。

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 

任何想法???

1 个答案:

答案 0 :(得分:0)

确定为您的实体创建表单类型并设置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',
    ));
}

现在在主窗体中添加新创建的窗体:

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

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