Symfony / Doctrine:OneToMany插入导致null id

时间:2017-12-29 22:10:49

标签: mysql symfony doctrine-orm symfony-2.8

我有两个实体,我正在尝试应用OneToMany / ManyToOne关系(一个游戏有很多GameContent)。

游戏

/**
 * @ORM\OneToMany(targetEntity="GameContent", mappedBy="game")
 */
private $contents;

public function __construct()
{
    $this->contents = new ArrayCollection();
}

public function getContents()
{
    return $this->contents;
}

GameContent

/**
 * @ORM\ManyToOne(targetEntity="Game", inversedBy="contents")
 */
private $game;

以下代码将两个记录都插入到各自的表中:

$game = $form->getData();
$content = new GameContent();
$content->setType('some type');
$game->getContents()->add($content);

$em = $this->getDoctrine()->getManager();
$em->persist($content);
$em->persist($game);
$em->flush();

但是,GameContent的game_id插入为null

INSERT INTO game_content (type, game_id) VALUES (?, ?)
Parameters: { 1: 'some type', 2: null }

我也试过了:

  • 更改persist()
  • 的顺序
  • 通过$game->getContents()->add($content)
  • $game->addContents($content)替换为$this->contents[] = $content;
  • 删除persist($content)并在游戏实体上拥有cascade={"persist"}

为什么game_id被插入为空?

我目前的解决方法是:

$em = $this->getDoctrine()->getManager();

$game = $form->getData();
$em->persist($game);

$content = new GameContent();
$content->setType('some type');
$content->setGame($game);
$em->persist($content);

$em->flush();

2 个答案:

答案 0 :(得分:0)

你有2个解决方案:

将孩子留在控制器中

没有cascade={"persist"}

$em = $this->getDoctrine()->getManager();

// Get data
$game = $form->getData();

// Create new GameContent and hydrate
$content = new GameContent();
$content->setType('some type');

// Associate Game <> GameContent
$content->setGame($game);

// Persist GameContent
$em->persist($content);

// Persist Game and commit
$em->persist($game);
$em->flush();

级联儿童

在OneToMany关系中使用cascade={"persist"}

添加setGame()功能,强制关联:

$game->addContent($this);

并删除持久性:

$em = $this->getDoctrine()->getManager();

// Get data
$game = $form->getData();

// Create new GameContent and hydrate
$content = new GameContent();
$content->setType('some type');

// Associate Game <> GameContent
$content->setGame($game);

// Persist Game and commit
$em->persist($game);
$em->flush();

我认为这个错误也是由于游戏中的持久性定位所致。

答案 1 :(得分:0)

除了接受的答案之外,我的下一步是创建一个表单来处理GameContent数据,这导致了进一步的更改和一些简化的逻辑。

我现在setGame() Game::addContent(),因此我已删除了$game->addContent($this);中的GameContent::setGame()

<强>游戏

/**
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="GameContent", mappedBy="game", cascade={"persist"})
 */
private $contents;

public function __construct()
{
    $this->contents = new ArrayCollection();
}

public function getContents()
{
    return $this->contents;
}

public function addContent(GameContent $content)
{
    $this->contents->add($content);
    $content->setGame($this);

    return $this;
}

public function removeContent(GameContent $content)
{
    $this->contents->removeElement($content);

    return $this;
}

<强> GameContent

/**
 * @ORM\ManyToOne(targetEntity="Game", inversedBy="contents")
 */
private $game;

public function setGame(Game $game)
{
    $this->game = $game;

    return $this;
}

/**
 * @return Game
 */
public function getGame()
{
    return $this->game;
}

现实世界的表单处理逻辑将如下所示:

$game = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($game);
$em->flush();

更多信息请访问:http://symfony.com/doc/2.8/form/form_collections.html(参见 Doctrine:级联关系并保存“反向”方)。