Vodokan
Vodokan

Reputation: 793

Populate form from database

For some reason I can't manage symfony to populate two dropdown lists with selected values from database. I have two entities category and product

CategoryType.php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CategoryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'entity', array(
            'class' => 'AppBundle\Entity\Category',
            'property' => 'name',
            'expanded' => false,
            'multiple' => false
        ));
    }


    public function configureOptions(OptionsResolver $options)
    {
        $options->setDefaults([
            'data_class' => 'AppBundle\Entity\Category',
        ]);
    }

    public function getName()
    {
        return 'app_category_type';
    }
}

ProductType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'entity', array(
            'class' => 'AppBundle\Entity\Product',
            'property' => 'name',
            'label' => 'hohoohoh',
            'data' => '',
            'expanded' => false,
            'multiple' => false
        ));
    }

    public function configureOptions(OptionsResolver $options)
    {
        $options->setDefaults([
            'data_class' => 'AppBundle\Entity\Product',
        ]);
    }

    public function getName()
    {
        return 'app_product_type';
    }
}

CommonType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Form\CategoryType;
use AppBundle\Form\ProductType;


class CommonType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $data = $builder->getData();

        $builder->add('category', new CategoryType());
        $builder->add('product', new ProductType());
    }



    public function getName()
    {
        return 'app_common_type';
    }
}

DefaultController.php

<?php

namespace AppBundle\Controller;

use AppBundle\Form\CommonType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {

        $product = $this->getDoctrine()->getRepository('AppBundle:Product')->findOneBy(['id' => 2]);


        $form = $this->createForm(new CommonType(), ['product' => $product]);

        return $this->render('AppBundle:Default:index.html.twig', ['form' => $form->createView()]);

    }
}

Dump of $product variable

DefaultController.php on line 20:
Product {#555 ▼
  #id: 2
  #name: "second"
  #price: "12.00"
  #description: "testing"
  #category: Category {#572 ▼
    +__isInitialized__: false
    -id: 2
    -name: null
    #products: null
     …2
  }
}

What I get as result is dropdown list with the values from the database but with no selected one. What I'm doing wrong and how to fix the issue?

Upvotes: 0

Views: 77

Answers (1)

Delphine
Delphine

Reputation: 888

I think there is a problem with both your productType and your categoryType.

You have not to create an entity field.

You should do instead (CommonType.php) :

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Form\CategoryType;
use AppBundle\Form\ProductType;


class CommonType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $data = $builder->getData();

        $builder->add('category', 'entity', array(
            'class' => 'AppBundle:Category',
            'property' => 'name',
            'label' => 'Category Name',
            'expanded' => false,
            'multiple' => false
            'placeholder' => 'Select',
            'required' => true
        ))
        ->add('product', 'entity', array(
            'class' => 'AppBundle:Product',
            'property' => 'name',
            'label' => 'ProductName',
            'expanded' => false,
            'multiple' => false
            'placeholder' => 'Select',
            'required' => true
        ));
    }



    public function getName()
    {
        return 'app_common_type';
    }
}

You also should to remove the data property ( 'data' => '', ). It is this one which make your select box empty instead of matchin category/product element.

UPDATE :

If you want to filter values in entity field you should use a queryBuilder.

'query_builder' => function(EntityRepository $er) use ($idValue) {
                    return $er->createQueryBuilder('p')
                              ->where('p.id = :idValue')
                              ->setParameter('idValue', $idValue);
                   },

Upvotes: 1

Related Questions