本文介绍了保存嵌入式收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体:

Game
Batting

游戏还有其他一些属性,日期位置
一个游戏有多个 Batting 实体,即一个板球
一个游戏具有多个属性, Runs ,解雇,玩家

A Game has several other properties, date, location etcA Game has several Batting Entites, i.e a game of cricketA Batting has several properties, 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',
  ))

您可以在下面看到类似的图片

You can see what this like like below

问题是添加击球并保存游戏时。

我希望 game_id 会保存在打击数据库表中,但始终为NULL。

I'd expect the game_id to be saved in the batting db table, but its always NULL.

任何想法我的代码有什么问题?

Any ideas what is wrong with my code?

谢谢

编辑:

已将代码更改为:

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;
}


推荐答案

您需要设置游戏

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

    $this->battings[] = $battings;

    return $this;
}

这篇关于保存嵌入式收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:25