Symfony2:实体的FormBuilder - optionsResolver

时间:2013-09-28 16:30:37

标签: php symfony entity formbuilder

我有3个实体:项目,用户,模块

模块实体获得了字段:

  1. $ ID
  2. $ user - 具有用户实体的多对一
  3. $ project - 项目实体的多对一
  4. $ modules - ArrayCollection
  5. ModuleController:

      $form = $this->createForm(new ModuleType($em), $module, array('user' => $userData, 'project' => $projectData));
    

    ModuleType是问题所在。我想只为具有$ modules字段的特定用户(userId)特定项目(projectId)构建表单集合。

    如何设置setDefaultsOptions以正确使用包含用户和项目选项的类Module(用于构建表单)?

    @Edit 好吧,这几乎是我想要的:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {  
        $transformer = new ModuleToArrayTransformer($this->entityManager);
        $builder->add('modules', 'choice', array(
            'multiple' => true,
            'expanded' => true,
            'choices' => array(array_keys($options['data'][0]->getModules()))
        ))
        ->addViewTransformer($transformer);
    }
    
    public function setObject($object)
    {
        $this->object = $object;
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {   
        $resolver->setDefaults(array(
            'data_class' => 'DevDash\ProjectBundle\Entity\Module',
            'project' => 'DevDash\ProjectBundle\Entity\Project',
            'user' => 'DevDash\ProjectBundle\User',
        ));
    }
    

    它使表格正确但所有框都未选中。知道为什么吗?

0 个答案:

没有答案