表单中的Symfony2实体字段


Symfony2 entity field in a form

我试图创建一个简单的表单来添加公司,但在使用实体时遇到了问题。

我使用公司类型实体添加了一个选择字段:

->add('idtypesociete', 'entity', array('class' => 'PromocastUtilisateurBundle:PcastTypesociete', 'property' => 'nomtypesociete'))

但当我提交表单时,我的idtypesociete字段包含一个"PcastTypesociete"对象,而不仅仅是所选选项的值。所以提交失败了。

我在我的公司实体和我的类型公司实体之间建立了多对一的关系,如下所示:

/**
 * @var integer $idtypesociete
 *
 * @ORM'Column(name="IDTYPESOCIETE", type="integer", nullable=false)
 * @ORM'ManyToOne(targetEntity="Promocast'UtilisateurBundle'Entity'PcastTypesociete")
 * @ORM'JoinColumns({
 *  @ORM'JoinColumn(name="PcastTypesociete_idtypesociete", referencedColumnName="idtypesociete")
 * })
 */
private $idtypesociete;

您是否有只获取所选公司类型的id的解决方案?(如果可能的话,不需要简单的sql请求来列出我的公司类型)

非常感谢!

如果关系正常,那么Symfony 2通常会很好地为您构建表单字段。

我认为问题在于$idtypesociete财产。你希望在水合实体上存储一个整数吗?

条令协会使用实体关系。您提供的注释决定了诸如联接列之类的幕后内容:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-到一个单向

我建议在做其他事情之前先备份或投入工作。

将实体属性更改为以下有帮助吗?

/**
 * @var PcastTypesociete $typesociete
 *
 * @ORM'Column(name="IDTYPESOCIETE", type="integer", nullable=false)
 * @ORM'ManyToOne(targetEntity="Promocast'UtilisateurBundle'Entity'PcastTypesociete")
 * @ORM'JoinColumns({
 *  @ORM'JoinColumn(name="PcastTypesociete_idtypesociete", referencedColumnName="idtypesociete")
 * })
 */
private $typesociete;

您可能需要通过条令更新数据库模式:模式:如果第一次不能正常工作,请使用控制台进行更新。您的实体也需要更新以反映新的物业名称。

如果这样做有效,那么您的表单应该只需要表单类型中的->add('typesociete'),并且您将有一个有效的实体选择字段,因为Symfony足够聪明,可以知道要使用什么字段类型。