1

我有 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',
  ))

你可以在下面看到它的样子

SonataAdmin 中的屏幕截图

问题是添加 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;
}
4

2 回答 2

4

您需要在每个击球对象上设置游戏。

public function addBatting(\CW\CricketBundle\Entity\Batting $battings)
{
    $battings->setGame($this);

    $this->battings[] = $battings;

    return $this;
}
于 2013-11-09T16:29:21.563 回答
1

好的,所以将我的管理表单类更改为以下内容:

        ->add('battings', 'sonata_type_collection',
            array(
                'by_reference' => false
            ),
            array(
                'edit' => 'inline',
                'inline' => 'table',
                'allow_delete' => true
            )
        )

似乎工作。现在当我保存时,game_id设置好了。

于 2013-11-09T17:40:16.647 回答