2013-11-09 65 views
1

我有2個實體:保存嵌入式收藏

Game 
Batting 

一個遊戲還有其他幾個特性,datelocation等 一個博弈有幾個Batting entites的,即板球 的遊戲擊球有幾種特性,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', 
)) 

你可以看到這是什麼樣像下面

Screenshot of above in SonataAdmin

的prblem添加Batting和保存Game是什麼時候。

我期望game_id保存在擊球數據庫表中,但它始終爲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; 
} 
+0

也許我的問題(和答案)可以幫助你:http://stackoverflow.com/questions/15629225/symfony2-1m-11-relationship-and-sonata-admin-form/15974241#15974241 –

回答

4

您需要設置遊戲每個擊球的對象。

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

    $this->battings[] = $battings; 

    return $this; 
} 
+0

那還沒有改變了任何東西,'game_id'仍然是NULL – user789122

+0

我已經用你提供的代碼更新了我的問題,也包括我的'setGame'方法 – user789122

1

好了,我chnging管理表單類以下幾點:

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

似乎工作。現在,當我保存時,設置了game_id