我在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"
));
}
}
Name
,Description
和Visible
都可以从数据库获取。但User
实体未使用userId
选择Product
。有人可以帮我这个吗?
答案 0 :(得分:1)
我解决了我的问题,在我的Product
实体中,User
实体的映射名称为users
。我将userId
更改为users
,并将其修复。