【发布时间】:2026-01-13 14:10:02
【问题描述】:
一个用户有多个服务并且想要添加一个服务用户。
这是我的实体:
/**
* @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] 执行“INSERT INTO proposal (prix, prix_main_oeuvre, type_id, nom_id, user_id) VALUES (?, ?, ?, ?, ?)' 时发生异常:
SQLSTATE[23000]:违反完整性约束:1062 键“UNIQ_C7CDC353C54C8C93”的重复条目“1”
谁能帮我看得更清楚?
【问题讨论】:
-
prix、prix_main_oeuvre、type_id、nom_id、user_id 之一具有
unique约束,因此您添加了重复条目。 -
'UNIQ_C7CDC353C54C8C93' 键用于 type_id @felipsmartins
标签: php symfony doctrine-orm