Victor Bocharsky
Victor Bocharsky

Reputation: 12306

How can I get ID of entity in form type?

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?

Upvotes: 3

Views: 8192

Answers (4)

Badarau Petru
Badarau Petru

Reputation: 59

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

Upvotes: -1

John Pancoast
John Pancoast

Reputation: 1271

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.

Upvotes: 2

ahmed hamdy
ahmed hamdy

Reputation: 5169

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);

Upvotes: 4

DonCallisto
DonCallisto

Reputation: 29912

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

Upvotes: 1

Related Questions