我在项目中使用学说。
我有一堂课Card
class Card implements \JsonSerializable
{
/**
* @ORM\Id
* @ORM\Column(type="string")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $sign;
/**
* @ORM\Column(type="string", length=10)
*/
private $number;
/**
* Card constructor.
*
* @param $sign
* @param $number
*/
public function __construct($sign, $number)
{
$this->sign = $sign;
$this->number = $number;
$this->id = \sprintf('%s_%s', $sign, $number);
}
}
还有一个CardPicker包含Card
class CardPicker
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* Card that could be picked.
*
* Many CardPicker have Many Card.
*
* @ManyToMany(targetEntity="Card", cascade={"persist", "remove"})
* @JoinTable(name="available_picker_cards",
* joinColumns={@JoinColumn(name="card_picker_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="card_id", referencedColumnName="id")}
* )
*/
private $availableCards;
public function setCards(array $cards): void
{
$this->availableCards = new ArrayCollection($cards);
}
}
我在我的数据库中保留了一些卡片。我想初始化我的CardPicker. 所以我写了这个方法:
public function initCardPicker(): CardPicker
{
// fetch all wanted cards and put them in CardPicker
$cards = $this->cardRepository->findAll();
$cardPicker = new CardPicker();
$cardPicker->setCards($cards);
return $this->repository->save($cardPicker);
}
但我收到了这个错误信息:SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: card.id (500 Internal Server
似乎当我将CardPicker它保存到re-persist我的卡片时,它在我的系统中应该是唯一的。
我在映射方面做错了吗?
千巷猫影