3

I have form type for build form of category:

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

How can I get category ID of entity in query_builder closure in buildForm action?

ahmed hamdy
  • 5,096
  • 1
  • 47
  • 58
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91

4 Answers4

4

answer for your question into this two questions symfony-2-how-to-pass-data-to-formbuilder and passing-data-from-controller-to-type-symfony2

1) create category variable and __construct() method into CategoryType class:

private category;
public function __construct(yourBundle\Category $category){

   $this->category = $category ;

}

2) use category variable in buildForm() method into CategoryType class :

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' => 'Корневая категория',
            ))

    }

finally when you create form in your controller :

$category = new Category();

$form = $this->createForm(new CategoryType($category),$category);
Community
  • 1
  • 1
ahmed hamdy
  • 5,096
  • 1
  • 47
  • 58
2

I don't know if these answers are as valid now. If you have the following code to create a form in your controller:

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

... then the MyFormType::buildForm() method will be passed the $options parameter which will have $options['data'] that contains the entity that you passed to createForm(), i.e., $fooEntity in this case. This is assuming you don't override the 'data' key option with your own value. So you should be able to get the entity's ID from that.

John Pancoast
  • 1,241
  • 16
  • 14
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
   }

And when you create your form, do something like

public myFooController() 
{
  //retrieve %category_id here
  $form = $this->creteForm(new CategoryType($category_id));
  [...]
}
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
-1

$options['data'] contains the entity for which the type is built.