Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving embedded collections
    primarykey
    data
    text
    <p>I have 2 Entities:</p> <pre><code>Game Batting </code></pre> <p>A Game has several other properties, <code>date</code>, <code>location</code> etc A Game has several <code>Batting</code> Entites, i.e a game of cricket A Batting has several properties, <code>Runs, Dismissal, Player</code></p> <p><code>Game.php</code></p> <pre><code> /** * * @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-&gt;battings[] = $battings; return $this; } </code></pre> <p><code>Batting.php</code></p> <pre><code> /** * @ORM\ManyToOne(targetEntity="CW\CricketBundle\Entity\Game", inversedBy="battings", cascade={"persist"}) * @ORM\JoinColumn(name="game_id", referencedColumnName="id") */ private $game; </code></pre> <p><code>GameAdmin.php</code></p> <pre><code>-&gt;with("Batting") -&gt;add('battings', 'sonata_type_collection', array(), array( 'edit' =&gt; 'inline', 'inline' =&gt; 'table', 'sortable' =&gt; 'id', )) </code></pre> <p>You can see what this like like below</p> <p><img src="https://i.stack.imgur.com/ZaXlR.jpg" alt="Screenshot of above in SonataAdmin"></p> <p>The prblem is when adding a <code>Batting</code> and saving the <code>Game</code>.</p> <p>I'd expect the <code>game_id</code> to be saved in the batting db table, but its always NULL.</p> <p>Any ideas what is wrong with my code?</p> <p>Thanks</p> <p>EDIT:</p> <p>Changed code to:</p> <pre><code>public function addBatting(\CW\CricketBundle\Entity\Batting $battings) { $battings-&gt;setGame($this); $this-&gt;battings[] = $battings; return $this; } </code></pre> <p>and</p> <pre><code>public function setGame(\CW\CricketBundle\Entity\Game $game) { $this-&gt;game = $game; return $this; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload