我正在使用 zf2 并且我有实体 DepositDict 可以有很多 periodTimeAmounts。每个 periodTimeAmount 可以有许多 periodMoney 元素。我可以使用许多 periodTimeAmounts 将我的实体保存到数据库中,但 periodMoney 仅保存最后一个 periodTimeAmount 实体。
存款字典
<?php
/*...*/
class DepositDict
{
/*...*/
/**
*
* @ORM\OneToMany(targetEntity="DepositDict\Entity\DepositDictPeriodTimeAmount", mappedBy="depositDict", cascade={"persist", "remove"})
* @var \Doctrine\Common\Collections\ArrayCollection
*/
protected $periodTimeAmounts;
public function __construct() {
$this->periodTimeAmounts = new ArrayCollection();
}
/*...*/
public function getPeriodTimeAmounts()
{
return $this->periodTimeAmounts;
}
public function addPeriodTimeAmounts(Collection $periodTimeAmounts)
{
if ( $this->getPeriodType() == self::PERIOD_TYPE_TIME_AMOUNT ) {
foreach ( $periodTimeAmounts as $periodTimeAmount )
{
$periodTimeAmount->setDepositDict($this);
$this->periodTimeAmounts->add($periodTimeAmount);
}
}
}
public function removePeriodTimeAmounts(Collection $periodTimeAmounts)
{
foreach ( $periodTimeAmounts as $periodTimeAmount )
{
$periodTimeAmount->setDepositDict(null);
$this->periodTimeAmounts->removeElement($periodTimeAmount);
}
}
}
?>
DepositDictPeriodTimeAmount
<?php
/*...*/
class DepositDictPeriodTimeAmount
{
/*...*/
/**
* @ORM\ManyToOne(targetEntity="DepositDict\Entity\DepositDict", inversedBy="periodTimeAmounts")
* @ORM\JoinColumn(name="deposit_dict_id", referencedColumnName="id")
*/
protected $depositDict;
/**
* @ORM\OneToMany(targetEntity="DepositDict\Entity\DepositDictPeriodMoney", mappedBy="periodTimeAmount", cascade={"persist", "remove"})
*/
protected $periodMoney;
public function __construct() {
$this->periodMoney = new ArrayCollection();
}
public function getDepositDict()
{
return $this->depositDict;
}
public function setDepositDict(DepositDict $depositDict=null)
{
if ( $depositDict === null || $depositDict instanceof DepositDict ) {
$this->depositDict = $depositDict;
}
else {
throw new \InvalidArgumentException('$depositDict must be instance of Entity\DepositDict');
}
}
public function getPeriodMoney()
{
return $this->periodMoney;
}
public function addPeriodMoney(Collection $moneyPeriods) //changed name because money is uncountable
{
foreach ( $moneyPeriods as $moneyPeriod )
{
$moneyPeriod->setPeriodTimeAmount($this);
$this->periodMoney->add($moneyPeriod);
}
}
public function removePeriodMoney(Collection $moneyPeriods)
{
foreach ( $moneyPeriods as $moneyPeriod )
{
$moneyPeriod->setPeriodTimeAmount(null);
$this->periodMoney->removeElement($moneyPeriod);
}
}
/*...*/
}
?>
存款字典期间钱
<?php
/*...*/
class DepositDictPeriodMoney
{
/*...*/
/**
* @ORM\ManyToOne(targetEntity="DepositDict\Entity\DepositDictPeriodTimeAmount", inversedBy="periodMoney")
* @ORM\JoinColumn(name="deposit_dict_period_time_amount_id", referencedColumnName="id")
*/
protected $periodTimeAmount;
/*...*/
public function getPeriodTimeAmount()
{
return $this->periodTimeAmount;
}
public function setPeriodTimeAmount(DepositDictPeriodTimeAmount $periodTimeAmount=null)
{
if ( $periodTimeAmount === null || $periodTimeAmount instanceof DepositDictPeriodTimeAmount ) {
$this->periodTimeAmount = $periodTimeAmount;
}
else {
throw new \InvalidArgumentException('$periodTimeAmount must be instance of Entity\DepositDictPeriodTimeAmount');
}
}
}
?>
少了什么东西?