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



我在项目中使用原则。

我上课Card

class Card implements JsonSerializable
{
/**
* @ORMId
* @ORMColumn(type="string")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $sign;
/**
* @ORMColumn(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);
}
}

以及包含Card的类CardPicker

class CardPicker
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(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我的卡,这在我的系统中应该是唯一的。

我在映射方面做错了什么吗?

编辑

存储库:

/**
* @method CardPicker|null find($id, $lockMode = null, $lockVersion = null)
* @method CardPicker|null findOneBy(array $criteria, array $orderBy = null)
* @method CardPicker[]    findAll()
* @method CardPicker[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CardPickerRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CardPicker::class);
}
/**
* Save cardPicker and return it.
*
* @throws DoctrineCommonPersistenceMappingMappingException
* @throws DoctrineORMORMException
* @throws DoctrineORMOptimisticLockException
*/
public function save(CardPicker $picker): CardPicker
{
$this->_em->persist($picker);
$this->_em->flush();
$this->_em->clear();
return $picker;
}
}

@grenierj我创建了类,它们为我工作。

创建实体后执行

php bin/console doctrine:migration:diff
php bin/console doctrine:migration:migrate

config/services.yaml

AppRepositoryCardRepository:
arguments:
- '@doctrine'
AppEntityCardPicker:
arguments:
- '@doctrine'
- 'AppRepositoryCardRepository'

应用/实体/卡片.php

<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryCardRepository")
*/
class Card
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="string")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $sign;
/**
* @ORMColumn(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);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @param string $id
* @return Card
*/
public function setId(string $id): Card
{
$this->id = $id;
return $this;
}
}

应用/实体/卡片选取器.php

<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryCardPickerRepository")
*/
class CardPicker
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* Card that could be picked.
*
* Many CardPicker have Many Card.
*
* @ORMManyToMany(targetEntity="Card", cascade={"persist", "remove"})
* @ORMJoinTable(name="available_picker_cards",
*     joinColumns={@ORMJoinColumn(name="card_picker_id", referencedColumnName="id")},
*     inverseJoinColumns={@ORMJoinColumn(name="card_id", referencedColumnName="id")}
* )
*/
private $availableCards;
public function __construct()
{
$this->availableCards = new ArrayCollection();
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
* @return CardPicker
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return mixed
*/
public function getAvailableCards()
{
return $this->availableCards;
}
/**
* @param mixed $availableCards
* @return CardPicker
*/
public function setAvailableCards(array $availableCards)
{
$this->availableCards = $availableCards;
return $this;
}
public function addAvailableCard(Card $card)
{
if (!$this->availableCards->contains($card)) {
$this->availableCards->add($card);
}
}
public function removeAvailableCard(Card $card)
{
$this->availableCards->removeElement($card);
}
}

App/Repository/CardPickerRepository.php

<?php
namespace AppRepository;
use AppEntityCardPicker;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use DoctrineCommonPersistenceManagerRegistry;
/**
* @method CardPicker|null find($id, $lockMode = null, $lockVersion = null)
* @method CardPicker|null findOneBy(array $criteria, array $orderBy = null)
* @method CardPicker[]    findAll()
* @method CardPicker[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CardPickerRepository extends ServiceEntityRepository
{
protected $cardRepository;
public function __construct(ManagerRegistry $registry, CardRepository $cardRepository)
{
$this->cardRepository = $cardRepository;
parent::__construct($registry, CardPicker::class);
}
public function initCardPicker(): CardPicker
{
$em = $this->getEntityManager();
$cards = $this->cardRepository->findAll();
$picker = new CardPicker();
$picker->setAvailableCards($cards);
$em->persist($picker);
$em->flush();
return $picker;
}
}

应用程序/存储库/卡存储库.php

<?php
namespace AppRepository;
use AppEntityCard;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use DoctrineCommonPersistenceManagerRegistry;
/**
* @method Card|null find($id, $lockMode = null, $lockVersion = null)
* @method Card|null findOneBy(array $criteria, array $orderBy = null)
* @method Card[]    findAll()
* @method Card[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CardRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Card::class);
}
}

相关内容

  • 没有找到相关文章

最新更新