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

时间:2014-02-23 13:42:52

标签: php symfony doctrine-orm

我有类别构建形式的表单类型:

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

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

3 个答案:

答案 0 :(得分:3)

在这两个问题symfony-2-how-to-pass-data-to-formbuilderpassing-data-from-controller-to-type-symfony2

中回答您的问题

1)在category类中创建__construct()变量和CategoryType方法:

private category;
public function __construct(yourBundle\Category $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);

答案 1 :(得分:1)

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));
  [...]
}

答案 2 :(得分:1)

我不知道这些答案现在是否有效。如果您具有以下代码以在控制器中创建表单:

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

...然后将向MyFormType::buildForm()方法传递$options参数,该参数将具有$options['data'],该参数包含您传递给createForm()的实体,即{{1} } 在这种情况下。这是假设您不会用自己的值覆盖“数据”键选项。因此,您应该能够从中获取实体的ID。