【问题标题】:Symfony2 entity field in a formSymfony2 表单中的实体字段
【发布时间】:2012-03-14 15:18:31
【问题描述】:

我正在尝试创建一个简单的表单来添加公司,但我在使用实体时遇到了麻烦。

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

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

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

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

/**
 * @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 请求来列出我的公司类型)

非常感谢!

【问题讨论】:

    标签: php forms symfony entity


    【解决方案1】:

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

    我认为问题在于 $idtypesociete 属性。您是否希望在水合实体上存储一个整数?

    学说关联使用实体关系。您提供的注释决定了幕后的东西,比如连接列: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional

    我建议在做任何其他事情之前备份或提交你的工作。

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

    /**
     * @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 足够聪明,知道要使用哪种字段类型。

    【讨论】:

    • 抱歉迟到了,但我终于成功了!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多