Symfony2 Form Builder Entity Type Selected Value

entityformssymfony

I am noop in symfony 2. Could someone help me for the symfony2 form type builder?

I have a form with Product Entity and I want to use selectbox in the form to list all user in my User Entity. When I created form with Product details, all inputs are ok without userid. User selectbox has not selected value which come from Product Entity with userId.

namespace ATL\ProductBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityRepository;

class ProductGeneralType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options){       
            $builder->add("name", null, array(
                "label" => "Name",
                "required" => true,
                "attr" => array(
                    "class" => "span5",
                )
            ))->add("description",null,array(
                "label" => "Description",
                "required" => true,
                "attr" => array(
                    "class" => "span8"
                )
            ))->add("visible",null,array(
                "label" => "Visible"
            ))->add("userId","entity",array(
                "label" => "User",
                "class" => "ATLUserBundle:User",
                "query_builder" => function(EntityRepository $er){
                    return $er->createQueryBuilder("u")->where("u.enabled = 1")->orderBy("u.username", "ASC");
                },
            ));
    }

    public function getName(){
        return "product";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefaults(array(
            "data_class" => "ATL\ProductBundle\Entity\Product"
        ));
    }   
}

Name, Description and Visible are ok and get from database. But User was not selected with userId from the Product Entity. Can someone help me with this?

Best Answer

I solved my problem, in my Product entity, my mapped name of User entity is users. I changed the userId to users, and it fixed.

Related Topic