Symfony2表单构建器实体类型选定的值

时间:2012-12-14 21:32:06

标签: forms symfony entity

我在symfony中是noop 2.有人可以帮我为symfony2表单类型构建器吗?

我有一个包含Product实体的表单,我想在表单中使用selectbox列出我的User实体中的所有用户。当我使用Product详细信息创建表单时,所有输入都没有userid。用户选择框未选择来自Product实体且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"
        ));
    }   
}

NameDescriptionVisible都可以从数据库获取。但User实体未使用userId选择Product。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

我解决了我的问题,在我的Product实体中,User实体的映射名称为users。我将userId更改为users,并将其修复。