我正在使用单表策略在项目中实现继承。
一切似乎在选择上都很好。但是当谈到更新时,我遇到了一个问题。
甲板:
<?php
//src/Deck.php
use Doctrine\ORM\EntityRepository;
/**
* @Entity(repositoryClass="DeckRepository")
* @Table(name="deck")
**/
class Deck
{
/** @id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $name;
/**
* @OneToMany(targetEntity="Mainboard", mappedBy="deck")
* @var Mainboard[]
**/
protected $mainboard = null;
/**
* @OneToMany(targetEntity="Sideboard", mappedBy="deck")
* @var Sideboard[]
**/
protected $sideboard = null;
/**
* @OneToMany(targetEntity="Maybeboard", mappedBy="deck")
* @var Maybeboard[]
**/
protected $maybeboard = null;
public function __construct() {
$this->createdon = new Datetime();
$this->mainboard = new \Doctrine\Common\Collections\ArrayCollection();
$this->sideboard = new \Doctrine\Common\Collections\ArrayCollection();
$this->maybeboard = new \Doctrine\Common\Collections\ArrayCollection();
}
[getters and setter]
?>
DeckEntry:
<?php
//src/DeckEntry.php
/**
* @Entity @Table(name="deck_card")
* @InheritanceType("SINGLE_TABLE")
* @id @DiscriminatorColumn(name="board", type="string")
* @DiscriminatorMap({"1" = "Mainboard", "2" = "Sideboard", "3" = "Maybeboard"})
**/
abstract class DeckEntry
{
/**
* @id @ManyToOne(targetEntity="Deck") @JoinColumn(referencedColumnName="id")
**/
protected $deck;
/**
* @id @ManyToOne(targetEntity="Card") @JoinColumn(referencedColumnName="multiverseid")
**/
protected $card;
/**
* @Column(type="integer")
**/
protected $quantity;
[getters/setters]
?>
主板/架/ Maybeboard:
<?php
//src/[Mainboard/Sideboard/Maybeboard].php
/**
* @Entity
*/
final class [Mainboard/Sideboard/Maybeboard] extends DeckEntry
{
}
?>
当我更新DeckEntry实例时,所有共享相同Deck和Card的DeckEntry实例都会在他们所属的板上更新。
这是我的更新代码:
switch ($_POST['board']) {
case 'mainboard' :
$deckEntry = $entityManager->getRepository('Mainboard')->find(array(
'deck' => $_POST['deck_id'],
'card' => $_POST['card_id']));
break;
case 'sideboard' :
$deckEntry = $entityManager->getRepository('Sideboard')->find(array(
'deck' => $_POST['deck_id'],
'card' => $_POST['card_id']));
break;
case 'maybeboard' :
$deckEntry = $entityManager->getRepository('Maybeboard')->find(array(
'deck' => $_POST['deck_id'],
'card' => $_POST['card_id']));
break;
}
$deckEntry->setQuantity($_POST['up_inv_qty']);
$entityManager->persist($deckEntry);
此代码生成以下选择和更新语句:
Query SELECT t0.quantity AS quantity1, t0.deck_id AS deck_id2, t0.card_id AS card_id3, t0.board FROM deck_card t0 WHERE t0.deck_id = '2' AND t0.card_id = '370772' AND t0.board IN ('1')
Query UPDATE deck_card SET quantity = '1' WHERE deck_id = '2' AND card_id = '370772'
我无法弄清楚为什么更新语句中缺少判别字段,因此使用匹配的Deck和Card更新所有DeckEntries。在不同的电路板中为现有的Deck和Card组合添加新的DeckEntry时也会发生这种情况。
非常感谢任何帮助!