【问题标题】:Form field (unique = true) and (nullable=true)表单字段 (unique = true) 和 (nullable=true)
【发布时间】:2016-09-08 07:13:55
【问题描述】:

我在 User.php 类中有一个名为“discountCode”的字段与“codiceSconto”相关。

//Discount.php
/**
 * @ORM\Column(type= "integer", nullable = true) 
 * @ORM\OneToMany(targetEntity= "User", mappedBy = "codiceSconto")
 */
 private discountCode;


//User.php
/**
 * @ORM\Column(type="integer", length=5, nullable=true)
 * @ORM\ManyToOne(targetEntity="Discount", inversedBy="discountCode")
 * @ORM\JoinColumn(name="codicesconto_id", referencedColumnName="id")
 */

private $codiceSconto;

它代表用户可以用来购买产品的折扣代码。 这不是必需的。 我将其设置为 (unique = true),因为每个折扣都代表一个折扣百分比,所以它必须是唯一的。但是当我尝试验证表单时出现错误,说明 discountCode 字段不能为 NULL。 有什么建议吗?

【问题讨论】:

    标签: forms symfony doctrine unique


    【解决方案1】:

    您错过了联接列中的nullable 选项。
    此外,如果您与其他实体有关系,则无需指定列类型。查看以下 sn-p 代码

    /**
     * @ORM\OneToMany(targetEntity= "User", mappedBy = "discount")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
     */
     private discountCode;
    

    【讨论】:

    • 嗯,没听懂。我已经用整个关系编辑了原始问题。我找不到错误。
    • @ZugZwang 正如我在这里所说,您应该将nullable 选项放在joinColumn 中,并且当您处于关系情况时,您不应该声明Column。请再读一遍我的回答。
    猜你喜欢
    • 2015-08-11
    • 1970-01-01
    • 2023-03-12
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多