Symfony表单构建器默认选择EntityType

时间:2016-07-14 06:42:44

标签: symfony-forms symfony

我尝试使用EntityType创建带有html select元素的表单。我必须通过某种条件获取值,并且通过此条件必需的默认值不从数据库中选择。所以我得到所有选项值,没有一个,必须是默认值。所以我试图找到一种方法来选择这个值。我尝试了什么...

以表格形式设定值:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $growing, ['user_id' => $user_id]);

$tableForm->get('garden')->setData($garden);
$form = $tableForm->createView()

然后我尝试在实体中设置数据:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$growing->setGarden($garden);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $growing, ['user_id' => $user_id]);
$form = $tableForm->createView()

然后我尝试使用'data'属性在form_builder中设置默认选择值:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $grow, [
            'user_id' => $user_id,
            'selected_choice' => $garden
        ]);
$form = $tableForm->createView();

Form_builder代码:

class GrowingType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('garden', EntityType::class , [
                'class' => 'FarmBundle\Entity\Garden',
                'query_builder' => function (GardenRepository $gr) use ($options) {
                    return $gr->queryFreeGardens($options['user_id']);
                },
                'attr' => [
                    'data-type' => 'text',
                    'class' => 'table-select',
                    'disabled' => true
                ],
                'required' => false,
                'data' => $options['selected_choice']
            ]);
     }

     /**
      * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FarmBundle\Entity\Growing',
            'selected_choice' => null,
            'user_id' => null
        ));
    }
 }

查询构建器的查询代码:

class GardenRepository extends \Doctrine\ORM\EntityRepository
{
    public function queryFreeGardens($user_id)
    {
        $qb = $this->createQueryBuilder('g')
            ->leftJoin('g.growing', 'grow')
            ->where('grow.plantDate is NULL')
            ->orWhere('grow.endDate is not NULL')
            ->andWhere('g.user = :user_id')
            ->orderBy('g.name')
           ->setParameter('user_id', $user_id);

    return $qb;
    }
}

所有这三种方法都行不通。结果是一个,如果实体未在查询构建器中获取查询,则无法设置此实体。如果我将实体设置为默认值,那么在查询构建器中一切都将正常工作 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这个 在控制器中:

$growing = $em->getRepository('FarmBundle:Growing')->findGrowing($user_id);
$garden = $em->getRepository('FarmBundle:Garden')->find(7);
$tableForm = $this->createForm('FarmBundle\Form\GrowingType', $grow, [
            'user_id' => $user_id,
            'additional_id' => 7
        ]);
$form = $tableForm->createView();

形式:

class GrowingType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('garden', EntityType::class , [
                'class' => 'FarmBundle\Entity\Garden',
                'query_builder' => function (GardenRepository $gr) use ($options) {
                    return $gr->queryFreeGardens($options['user_id'], $options['additional_id');
                },
                'attr' => [
                    'data-type' => 'text',
                    'class' => 'table-select',
                    'disabled' => true
                ],
                'required' => false
            ]);
     }

     /**
      * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FarmBundle\Entity\Growing',
            'additional_id' => null,
            'user_id' => null
        ));
    }
 }
存储库中的

class GardenRepository extends \Doctrine\ORM\EntityRepository
{
    public function queryFreeGardens($user_id, $additional_id)
    {
        $qb = $this->createQueryBuilder('g')
            ->leftJoin('g.growing', 'grow')
            ->where('grow.plantDate is NULL')
            ->andWhere('g.id = :additional_id')
            ->orWhere('grow.endDate is not NULL')
            ->andWhere('g.user = :user_id')
            ->andWhere('g.id = :additional_id')  
            ->orderBy('g.name')
           ->setParameter('user_id', $user_id)->setParameter('additional_id', $additional_id);

    return $qb;
    }
}

也许您需要调整存储库方法以正确的方式检索值。有或者子句,你应该将这个额外的id添加到你的或子句的两个分支。主要思想是检索您选择的对象。