Php Symfony2表单多个实体类型

Php Symfony2表单多个实体类型,php,symfony,Php,Symfony,这是实体: class MyEntity { /** * @var \OtherEntity * * @ORM\ManyToOne(targetEntity="OtherEntity") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="otherentity_id", referencedColumnName="id") * }) */ private $other

这是实体

class MyEntity {
    /**
     * @var \OtherEntity
     *
     * @ORM\ManyToOne(targetEntity="OtherEntity")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="otherentity_id", referencedColumnName="id")
     * })
     */
    private $otherentity;

   // some other fields
}
class MyEntityType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        parent::buildForm($builder, $options);
        $builder->add('name', 'text');
        // HOW TO ADD THE ENTITY TO JOIN THE ADDED MyEntity with the OtherEntity (with ID=5)?
       // i tried this:
       ->add('otherentity', 'entity',
           array('class' => 'My\MyTestBundle\Entity\OtherEntity',
                  'read_only' => true,
                  'property' => 'id',
                  'query_builder' => function (
                \Doctrine\ORM\EntityRepository $repository) {
               return $repository->createQueryBuilder('o')
                           ->where('o.id = ?1')
                       ->setParameter(1, 5);
    }
)
我的控制器的操作:

someAction(Request $request) {
    $em = $this->getDoctrine()->getEntityManager();
    // simplified this step here with id=5, so that all Entities of class MyEntity a link to the OtherEntity with ID=5 
    $otherEntity = $this->getDoctrine()->getRepository('MyTestBundle:OtherEntity')->find(5);

    $myEntity = new MyEntity();
    $myEntity->setOtherEntity($otherEntity);

    $form = $this->createForm(new MyEntityType(), $myEntity);
    // do some form stuff like isValid, isMethod('POST') etc.
}
这是表单类型

class MyEntity {
    /**
     * @var \OtherEntity
     *
     * @ORM\ManyToOne(targetEntity="OtherEntity")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="otherentity_id", referencedColumnName="id")
     * })
     */
    private $otherentity;

   // some other fields
}
class MyEntityType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        parent::buildForm($builder, $options);
        $builder->add('name', 'text');
        // HOW TO ADD THE ENTITY TO JOIN THE ADDED MyEntity with the OtherEntity (with ID=5)?
       // i tried this:
       ->add('otherentity', 'entity',
           array('class' => 'My\MyTestBundle\Entity\OtherEntity',
                  'read_only' => true,
                  'property' => 'id',
                  'query_builder' => function (
                \Doctrine\ORM\EntityRepository $repository) {
               return $repository->createQueryBuilder('o')
                           ->where('o.id = ?1')
                       ->setParameter(1, 5);
    }
)
) // ... 其他一些领域 } //标准模板法等。 }

所以我的问题是,我必须为$builder->add选择什么来添加其他实体,所以如果我在控制器中执行
$em->persist($myEntity)
来通过表单持久化添加的myEntity,那么我的数据库中会有这样的记录:

id | name   | otherentity_id
1  | 'test' | 5
注意:我不想保留一个新的otherEntity,我只想创建一个新的MyEntity并添加一个otherEntity的外键。

你不能这样使用它吗:

$builder->add('otherentity', 'entity', array(
    'class' => 'MyTestBundle:OtherEntity'
));
你就不能用这样的方法:

$builder->add('otherentity', 'entity', array(
    'class' => 'MyTestBundle:OtherEntity'
));

我原以为这样就可以了,但我得到了一个“传递到选择字段的实体必须被管理。也许在实体管理器中保留它们。”我编辑了我的问题。您可以找到上面的选项。(我也在没有Querybuilder的情况下尝试过…)只需要
class
选项,应该使用Symfony速记符号来指定,而不是使用示例代码中使用的完整PHP名称空间+类名。我编辑了我的答案来展示这一点。我明天会尝试,并让你知道它是否有效。晚安。我原以为这样就可以了,但我得到了一个“传递到选择字段的实体必须进行管理。可能将它们保留在实体管理器中”我编辑了我的问题。您可以找到上面的选项。(我也在没有Querybuilder的情况下尝试过…)只需要
class
选项,应该使用Symfony速记符号来指定,而不是使用示例代码中使用的完整PHP名称空间+类名。我编辑了我的答案来展示这一点。我明天会尝试,并让你知道它是否有效。晚安