获取实体列表,然后将它们设置为多对多关系

我在项目中使用学说。


我有一堂课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我的卡片时,它在我的系统中应该是唯一的。


我在映射方面做错了吗?


胡子哥哥
浏览 80回答 1
1回答

千巷猫影

创建实体后执行php bin/console doctrine:migration:diffphp bin/console doctrine:migration:migrate配置/服务.yaml&nbsp; &nbsp; App\Repository\CardRepository:&nbsp; &nbsp; &nbsp; &nbsp; arguments:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - '@doctrine'&nbsp; &nbsp; App\Entity\CardPicker:&nbsp; &nbsp; &nbsp; &nbsp; arguments:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - '@doctrine'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - 'App\Repository\CardRepository'应用程序/实体/Card.php<?phpnamespace App\Entity;use Doctrine\ORM\Mapping as ORM;/**&nbsp;* @ORM\Entity(repositoryClass="App\Repository\CardRepository")&nbsp;*/class Card{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ORM\Id()&nbsp; &nbsp; &nbsp;* @ORM\GeneratedValue()&nbsp; &nbsp; &nbsp;* @ORM\Column(type="string")&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $id;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ORM\Column(type="string", length=255)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $sign;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ORM\Column(type="string", length=10)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $number;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Card constructor.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param $sign&nbsp; &nbsp; &nbsp;* @param $number&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct($sign, $number)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->sign = $sign;&nbsp; &nbsp; &nbsp; &nbsp; $this->number = $number;&nbsp; &nbsp; &nbsp; &nbsp; $this->id = \sprintf('%s_%s', $sign, $number);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getId(): string&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->id;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param string $id&nbsp; &nbsp; &nbsp;* @return Card&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setId(string $id): Card&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->id = $id;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }}应用程序/实体/CardPicker.php<?phpnamespace App\Entity;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\ORM\Mapping as ORM;/**&nbsp;* @ORM\Entity(repositoryClass="App\Repository\CardPickerRepository")&nbsp;*/class CardPicker{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ORM\Id()&nbsp; &nbsp; &nbsp;* @ORM\GeneratedValue()&nbsp; &nbsp; &nbsp;* @ORM\Column(type="integer")&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $id;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Card that could be picked.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* Many CardPicker have Many Card.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @ORM\ManyToMany(targetEntity="Card", cascade={"persist", "remove"})&nbsp; &nbsp; &nbsp;* @ORM\JoinTable(name="available_picker_cards",&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;joinColumns={@ORM\JoinColumn(name="card_picker_id", referencedColumnName="id")},&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;inverseJoinColumns={@ORM\JoinColumn(name="card_id", referencedColumnName="id")}&nbsp; &nbsp; &nbsp;* )&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $availableCards;&nbsp; &nbsp; public function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->availableCards = new ArrayCollection();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getId()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->id;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param mixed $id&nbsp; &nbsp; &nbsp;* @return CardPicker&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setId($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->id = $id;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getAvailableCards()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->availableCards;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param mixed $availableCards&nbsp; &nbsp; &nbsp;* @return CardPicker&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setAvailableCards(array $availableCards)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->availableCards = $availableCards;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; public function addAvailableCard(Card $card)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!$this->availableCards->contains($card)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->availableCards->add($card);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public function removeAvailableCard(Card $card)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->availableCards->removeElement($card);&nbsp; &nbsp; }}应用程序/存储库/CardPickerRepository.php<?phpnamespace App\Repository;use App\Entity\CardPicker;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\Common\Persistence\ManagerRegistry;/**&nbsp;* @method CardPicker|null find($id, $lockMode = null, $lockVersion = null)&nbsp;* @method CardPicker|null findOneBy(array $criteria, array $orderBy = null)&nbsp;* @method CardPicker[]&nbsp; &nbsp; findAll()&nbsp;* @method CardPicker[]&nbsp; &nbsp; findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)&nbsp;*/class CardPickerRepository extends ServiceEntityRepository{&nbsp; &nbsp; protected $cardRepository;&nbsp; &nbsp; public function __construct(ManagerRegistry $registry, CardRepository $cardRepository)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->cardRepository = $cardRepository;&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct($registry, CardPicker::class);&nbsp; &nbsp; }&nbsp; &nbsp; public function initCardPicker(): CardPicker&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $em = $this->getEntityManager();&nbsp; &nbsp; &nbsp; &nbsp; $cards = $this->cardRepository->findAll();&nbsp; &nbsp; &nbsp; &nbsp; $picker = new CardPicker();&nbsp; &nbsp; &nbsp; &nbsp; $picker->setAvailableCards($cards);&nbsp; &nbsp; &nbsp; &nbsp; $em->persist($picker);&nbsp; &nbsp; &nbsp; &nbsp; $em->flush();&nbsp; &nbsp; &nbsp; &nbsp; return $picker;&nbsp; &nbsp; }}应用程序/存储库/CardRepository.php<?phpnamespace App\Repository;use App\Entity\Card;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\Common\Persistence\ManagerRegistry;/**&nbsp;* @method Card|null find($id, $lockMode = null, $lockVersion = null)&nbsp;* @method Card|null findOneBy(array $criteria, array $orderBy = null)&nbsp;* @method Card[]&nbsp; &nbsp; findAll()&nbsp;* @method Card[]&nbsp; &nbsp; findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)&nbsp;*/class CardRepository extends ServiceEntityRepository{&nbsp; &nbsp; public function __construct(ManagerRegistry $registry)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct($registry, Card::class);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP