Symfony2 EntityType字段保持不变

时间:2015-12-10 08:24:01

标签: symfony doctrine-orm

我有实体:AdWCTypeAdWallType。这些实体包含静态数据,如目录。

AdWallType

index  title
-----  -----
1      Brick
2      Plate

实体Ad包含wallTypewcType列。

ADTYPE

$builder->add('wallType', EntityType::class, array(
                'class' => 'AdBundle:AdWallType',
                'choice_label' => 'title',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('wt')
                        ->where('wt.visible = :visible')
                        ->setParameter('visible', true)
                        ->orderBy('wt.weight', 'ASC')
                        ->setCacheable(true);
                }
            ))
            ->add('wcType', EntityType::class, array(
                'class' => 'AdBundle:AdWCType',
                'choice_label' => 'title',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('wc')
                        ->where('wc.visible = :visible')
                        ->setParameter('visible', true)
                        ->orderBy('wc.weight', 'ASC')
                        ->setCacheable(true);
                }
            ))

如何在表单提交中将我选择的wcTypewallType保留到实体Ad中?

1 个答案:

答案 0 :(得分:0)

为此字段添加关联

/**
 * @var ...
 *
 * @ORM\wallType(targetEntity="AdBundle:AdWallType")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="wallType", referencedColumnName="index")
 * })
 */
private $wallType;
...

并保存表单数据对象

$ad = new Ad();
$form = $this->createForm('%form_class%', $ad);
if ($request->isMethod('post')) {
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em->persist($ad);
        $em->flush($ad);
    }
}