私は zf2 を使用しており、多くの periodTimeAmounts を持つことができるエンティティ DepositDict があります。各 periodTimeAmount には、多くの periodMoney 要素を含めることができます。多くの periodTimeAmounts を持つエンティティを DB に保存できますが、 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);
}
}
/*...*/
}
?>
DepositDictPeriodMoney
<?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');
}
}
}
?>
何が欠けていますか?