3

我是 symfony 2 的新手。有人可以帮我构建 symfony2 表单类型构建器吗?

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

NameDescription并且Visible可以从数据库中获取。但User没有userIdProduct实体中选择。有人可以帮我弄这个吗?

4

1 回答 1

1

我解决了我的问题,在我的Product实体中,我的实体映射名称Userusers. 我将其更改userIdusers,它已修复。

于 2012-12-15T13:04:28.307 回答