我有 2 个实体:
Game
Batting
一个游戏有几个其他属性,date
等等location
一个游戏有几个Batting
实体,即一个板球比赛一个击球有几个属性,Runs, Dismissal, Player
Game.php
/**
*
* @ORM\OneToMany(targetEntity="Batting", mappedBy="game", cascade={"persist", "remove"})
*/
private $battings;
/**
* Add battings
*
* @param \CW\CricketBundle\Entity\Batting $battings
* @return Game
*/
public function addBatting(\CW\CricketBundle\Entity\Batting $battings)
{
$this->battings[] = $battings;
return $this;
}
Batting.php
/**
* @ORM\ManyToOne(targetEntity="CW\CricketBundle\Entity\Game", inversedBy="battings", cascade={"persist"})
* @ORM\JoinColumn(name="game_id", referencedColumnName="id")
*/
private $game;
GameAdmin.php
->with("Batting")
->add('battings', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'id',
))
你可以在下面看到它的样子
问题是添加 aBatting
并保存Game
.
我希望将game_id
其保存在 batting db 表中,但它始终为 NULL。
任何想法我的代码有什么问题?
谢谢
编辑:
将代码更改为:
public function addBatting(\CW\CricketBundle\Entity\Batting $battings)
{
$battings->setGame($this);
$this->battings[] = $battings;
return $this;
}
和
public function setGame(\CW\CricketBundle\Entity\Game $game)
{
$this->game = $game;
return $this;
}