如何在表单类型中获取实体的ID



我有用于构建类别表单的表单类型:

class CategoryType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('published', 'checkbox', array(
                    'required' => FALSE,
                ))
                ->add('parent', 'entity', array(
                    'class' => 'BWBlogBundle:Category',
                    'property' => 'name',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                                ->where('c.id != :id')
                                ->setParameter('id', ... /* I need to get category ID here */)
                            ;
                    },
                    'required' => FALSE,
                    'empty_value' => 'Корневая категория',
                ))
            // other my code

如何在buildForm操作中获取query_builder关闭中实体的类别 ID?

回答

你的问题到这两个问题symfony-2-how-to-pass-data-to-formbuilder和传递数据从控制器到类型-symfony2

1) 将category变量和__construct()方法创建到类CategoryType

private category;
public function __construct(yourBundleCategory $category){
   $this->category = $category ;
}

2)在buildForm()方法中使用类别变量到CategoryType类中:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $category = $this->category;
    $builder
            ->add('published', 'checkbox', array(
                'required' => FALSE,
            ))
            ->add('parent', 'entity', array(
                'class' => 'BWBlogBundle:Category',
                'property' => 'name',
                'query_builder' => function(EntityRepository $er) use ($category){
                    return $er->createQueryBuilder('c')
                            ->where('c.id != :id')
                            ->setParameter('id', $category->getId())
                        ;
                },
                'required' => FALSE,
                'empty_value' => 'Корневая категория',
            ))
    }

最后,当您在控制器中创建表单时:

$category = new Category();
$form = $this->createForm(new CategoryType($category),$category);
我不知道

这些答案现在是否有效。如果您有以下代码在控制器中创建窗体:

$fooEntity = $entityManager->find(FooEntity::class, 123);
$form = $this->createForm(MyFormType::class, $fooEntity);

。然后,MyFormType::buildForm()方法将被传递给 $options 参数,该参数将具有包含您传递给 createForm() 的实体的$options['data'],即在本例中为 $fooEntity。这是假设您没有用自己的值覆盖"data"键选项。因此,您应该能够从中获取实体的ID。

class CategoryType extends AbstractType
{
  private $category_id;
  public function __construct($category_id=null) 
  {
    $this->category_id = $category_id;
  }
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
        $builder
                ->add('published', 'checkbox', array(
                    'required' => FALSE,
                ))
                ->add('parent', 'entity', array(
                    'class' => 'BWBlogBundle:Category',
                    'property' => 'name',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                                ->where('c.id != :id')
                                ->setParameter('id', $this->category_id) /* I need to get category ID here */)
                            ;
                    },
                    'required' => FALSE,
                    'empty_value' => 'Корневая категория',
                ))
            // other my code
   }

当您创建表单时,请执行类似操作

public myFooController() 
{
  //retrieve %category_id here
  $form = $this->creteForm(new CategoryType($category_id));
  [...]
}

$options['data'] 包含为其生成类型的实体。

相关内容

  • 没有找到相关文章

最新更新