执行“插入”命题“时发生异常


An exception occurred while executing 'INSERT INTO proposition

用户有多个服务并希望添加服务用户的事实。
这是我的实体:

/**
 * @ORM'Entity
 * @ORM'Table(name="user")
 */
class User extends BaseUser
{
    /**
     * @ORM'OneToMany(targetEntity="bundle'FrontBundle'Entity'Proposition", mappedBy="user", cascade={"persist"})
     * @ORM'JoinColumn(nullable=true)
     */
    protected $propositions;
    public function __construct()
    {
        $this->proposition = new ArrayCollection();
    }
}
/**
 * Proposition
 *
 * @ORM'Table(name="proposition")
 * @ORM'Entity
 */
class Proposition
{
    /**
     * @var int
     *
     * @ORM'Column(name="id", type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM'OneToOne(targetEntity="bundle'FrontBundle'Entity'Type", cascade={"persist", "remove"})
     * @ORM'JoinColumn(nullable=false)
     */
    protected $type;
    /**
     * @ORM'OneToOne(targetEntity="bundle'FrontBundle'Entity'NomPrestation", cascade={"persist", "remove"})
     * @ORM'JoinColumn(nullable=false)
     */
    protected $nom;
    /**
     * @ORM'ManyToOne(targetEntity="bundle'FrontBundle'Entity'User", inversedBy="propositions", cascade={"persist", "merge"})
     * @ORM'JoinColumn(nullable=false)
     */
    protected $user;
}

/**
 * Prestation
 *
 * @ORM'Table(name="type")
 * @ORM'Entity
 */
class Type
{
    /**
     * @var int
     *
     * @ORM'Column(name="id", type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     *
     * @ORM'Column(name="type", type="string", length=255)
     */
    protected $type;
}

/**
 * TypePrestation
 *
 * @ORM'Table(name="nom_prestation")
 * @ORM'Entity
 */
class NomPrestation
{
    /**
     * @var int
     *
     * @ORM'Column(name="id", type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     *
     * @ORM'Column(name="nom", type="string", length=255)
     */
    protected $nom;
  }

我的问题是这个,当我添加第一个提案并且当我添加另一个提案时一切正常时,我遇到了这个错误。

使用参数 [3442, 232, 1, 4, 1] 执行"插入命题(prix, prix_main_oeuvre, type_id, nom_id, user_id) 值 (?, ?, ?, ?, ?)"时发生异常:

SQLSTATE[23000]:完整性约束冲突:1062 键"UNIQ_C7CDC353C54C8C93"的重复条目"1"

有人可以帮我看得更清楚吗?

您遇到此错误是因为 id 1的类型已存在。

我想你想多次重复使用相同的类型。如果是这样,则必须将PropositionType之间的关系从oneToOne更改为oneToMany

我怀疑这同样适用于你的NomPrestation关系。如果不重用这些NomPrestation,则应考虑将其添加为Proposition的属性而不是实体。