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?