1 回答
TA贡献1829条经验 获得超7个赞
创建实体后执行
php bin/console doctrine:migration:diff
php bin/console doctrine:migration:migrate
配置/服务.yaml
App\Repository\CardRepository:
arguments:
- '@doctrine'
App\Entity\CardPicker:
arguments:
- '@doctrine'
- 'App\Repository\CardRepository'
应用程序/实体/Card.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CardRepository")
*/
class Card
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @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);
}
/**
* @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;
}
}
应用程序/实体/CardPicker.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CardPickerRepository")
*/
class CardPicker
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* Card that could be picked.
*
* Many CardPicker have Many Card.
*
* @ORM\ManyToMany(targetEntity="Card", cascade={"persist", "remove"})
* @ORM\JoinTable(name="available_picker_cards",
* joinColumns={@ORM\JoinColumn(name="card_picker_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(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);
}
}
应用程序/存储库/CardPickerRepository.php
<?php
namespace App\Repository;
use App\Entity\CardPicker;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @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;
}
}
应用程序/存储库/CardRepository.php
<?php
namespace App\Repository;
use App\Entity\Card;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @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);
}
}
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报