Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony2 EntityType字段持久化_Symfony_Doctrine Orm - Fatal编程技术网

Symfony2 EntityType字段持久化

Symfony2 EntityType字段持久化,symfony,doctrine-orm,Symfony,Doctrine Orm,我有实体:AdWCType和AdWallType。这些实体包含静态数据,如目录 AdWallType index title ----- ----- 1 Brick 2 Plate $builder->add('wallType', EntityType::class, array( 'class' => 'AdBundle:AdWallType', 'choice_label' => '

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

AdWallType

index  title
-----  -----
1      Brick
2      Plate
$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);
                }
            ))
实体
Ad
包含列
wallType
wcType

AdType

index  title
-----  -----
1      Brick
2      Plate
$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);
                }
            ))

如何在表单提交时将选择的
wcType
wallType
保存到实体
Ad

添加此字段的关联

/**
 * @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);
    }
}