在实体字段类型中选择的选项

时间:2014-07-10 17:39:22

标签: symfony

我有这个实体,使用查询构建器我得到一个选项列表,在select中显示,然后我想选择其中一个选项。我正在使用'数据'来选择id = 1的选项,但它不起作用,有关如何选择选项的任何想法吗? 感谢

   public function buildForm(FormBuilderInterface $builder, array $options)
{
    if($options['project'] instanceof Cpj\ProjectsBundle\Entity\Project){
        $projectId = $options['project']->getId();
    }
    else{
        $projectId = null;
    }

    if($options['sprint'] instanceof Cpj\ProjectsBundle\Entity\Sprint ){
        $sprintId = $options['sprint']->getId();
    }
    else{
        $sprintId = null;
    }

    $builder
        ->add('type', 'choice', array(
            'choices' => array(
                ''          =>  '--',
                'new'       =>  'New',
                'bugfix'    =>  'Bugfix'
            )))
        ->add('name', 'text')
        ->add('project', 'entity', array(
            'class' =>  'CpjProjectsBundle:Project',
            'property'  =>  'name',
            'empty_value' => '--',
            'empty_data'  => null,
            'required'      => false,
            'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->where("u.user = :id_user")
                        ->setParameter('id_user', $this->user);
                },
            'data'  => $projectId,
        ))
        ->add('sprint', 'entity', array(
            'class' =>  'CpjProjectsBundle:Sprint',
            'property'  =>  'name',
            'empty_value' => '--',
            'empty_data'  => null,
            'required'      => false,
            'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->where("u.user = :id_user")
                        ->setParameter('id_user', $this->user);
                },
            'data'  => $sprintId,
        ))
        ->add('importance', 'text')
        ->add('storypoints', 'text')
        ->add('description', 'textarea')
        ->add('howtoprove', 'textarea')
        ->add('save', 'submit');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'project'   => null,
        'sprint'    =>  null,
    ));
}

public function __construct ($user)
{
    $this->user          = $user;
}

public function getName()
{
    return 'story';
}

这是我正在使用的动作

    public function newAction(Request $request)
{
    $user = $this->getUser();
    $story = new Story();

    $id_project = $request->query->get('project');
    $id_sprint = $request->query->get('sprint');

    $form = $this->createForm(new StoryType($user), $story);

    $form->getData()->getProject()->setValue($id_project);
    $form->getData()->getSprint()->setValue($id_sprint);
    $form->setData($form->getData());

    $project = $this->getDoctrine()->getRepository('CpjProjectsBundle:Project')->findOneBy(array(
        'id'        =>  $id_project,
        'user'   =>  $user,
    ));
    $sprint = $this->getDoctrine()->getRepository('CpjProjectsBundle:Sprint')->findOneBy(array(
        'id'        =>  $id_sprint,
        'user'   =>  $user,
    ));
    $form = $this->createForm(new StoryType($user), $story, array(
        'project' => $project,
        'sprint'    => $sprint,
    ));
    $form->handleRequest($request);
    if($form->isValid())
    {
        $story->setUser($user);
        $em = $this->getDoctrine()->getManager();
        $em->persist($story);
        $em->flush();
        return $this->redirect($this->generateUrl('cpj_stories_homepage'));
    }
    return $this->render('CpjProjectsBundle:Story:new.html.twig', array(
        'form' => $form->createView(),
    ));
}

1 个答案:

答案 0 :(得分:0)

您可以尝试在新故事实体上设置项目和sprint,然后再将其传递给表单创建。像这样:

public function newAction(Request $request)
{
    $user = $this->getUser();
    $story = new Story();

    $id_project = $request->query->get('project');
    $id_sprint = $request->query->get('sprint');

    $project = $this->getDoctrine()->getRepository('CpjProjectsBundle:Project')->findOneBy(array(
        'id'        =>  $id_project,
        'user'   =>  $user,
    ));
    $sprint = $this->getDoctrine()->getRepository('CpjProjectsBundle:Sprint')->findOneBy(array(
        'id'        =>  $id_sprint,
        'user'   =>  $user,
    ));

    $story->setProject($project);
    $story->setSprint($sprint);

    $form = $this->createForm(new StoryType($user), $story);

    $form->handleRequest($request);
    if($form->isValid())
    {
        $story->setUser($user);
        $em = $this->getDoctrine()->getManager();
        $em->persist($story);
        $em->flush();
        return $this->redirect($this->generateUrl('cpj_stories_homepage'));
    }
    return $this->render('CpjProjectsBundle:Story:new.html.twig', array(
        'form' => $form->createView(),
    ));
}

然后在你的表格中看起来像:

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('type', 'choice', array(
            'choices' => array(
                ''          =>  '--',
                'new'       =>  'New',
                'bugfix'    =>  'Bugfix'
            )))
        ->add('name', 'text')
        ->add('project', 'entity', array(
            'class' =>  'CpjProjectsBundle:Project',
            'property'  =>  'name',
            'empty_value' => '--',
            'empty_data'  => null,
            'required'      => false,
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->where("u.user = :id_user")
                    ->setParameter('id_user', $this->user);
            },
        ))
        ->add('sprint', 'entity', array(
            'class' =>  'CpjProjectsBundle:Sprint',
            'property'  =>  'name',
            'empty_value' => '--',
            'empty_data'  => null,
            'required'      => false,
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->where("u.user = :id_user")
                    ->setParameter('id_user', $this->user);
            }
        ))
        ->add('importance', 'text')
        ->add('storypoints', 'text')
        ->add('description', 'textarea')
        ->add('howtoprove', 'textarea')
        ->add('save', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'CpjProjectsBundle:Story'
    ));
}

您还可以在表单

中取出$builder->add()...以上的所有内容