2013-05-22 29 views
0

我正在使用zf2,我有實體DepositDict可以有很多periodTimeAmounts。每個periodTimeAmount可以有多個periodMoney元素。我可以使用多個periodTimeAmounts將實體保存到數據庫,但periodMoney僅保存在最後一個periodTimeAmount實體中。在學說2中如何設置可以擁有自己收藏的藏品?

DepositDict

<?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'); 
    } 
} 

} 
?> 

缺少什麼?

+0

if($ depositDict === null || $ depositDict instanceof DepositDict){ $ this-> depositDict = $ depositDict; } else { throw new \ InvalidArgumentException('$ depositDict必須是Entity \ DepositDict的實例'); } – nifr

+0

這段代碼是不必要的,因爲類型檢查已經由php在公共函數setDepositDict(DepositDict $ depositDict = null)中完成。考慮創建DepositDictInterface以便更容易地擴展! – nifr

回答

0

重要

學說將只檢查關聯的擁有方更改。

僅針對關聯反面進行的更改將被忽略。確保更新雙向關聯的雙方(或至少從主體的角度來看擁有方)

  • OneToMany協會永遠不是自己的一面。
  • 反面必須使用OneToOne,OneToMany或ManyToMany映射聲明的mappedBy屬性。 mappedBy屬性包含擁有側的關聯字段的名稱
  • 擁有端必須使用OneToOne,ManyToOne或ManyToMany映射聲明的inversedBy屬性。 inversedBy屬性包含反面的關聯字段的名稱。
  • ManyToOne始終是雙向關聯的擁有者。
  • OneToMany始終是雙向關聯的反面。

集合中的新實體不會自動由教義管理。

首先請確保您在刷新之前調用persist。

$em->persist($entity); 
$em->flush(); 

你已經有級聯堅持刪除選項設置 - 這是好的。

接下來要嘗試的是將合併選項添加到您的級聯中。 如果相關的實體是一些振振有辭那麼超脫,他們將被合併,例如:

/** 
* @ORM\OneToMany(targetEntity="DepositDict\Entity\DepositDictPeriodMoney", mappedBy="periodTimeAmount", cascade={"persist", "remove", "merge"}) 
*/ 

合併:合併級聯到相關實體的操作。

+0

我已經創建了使用上述所有規則的實體,我也嘗試添加合併選項,但它沒有解決我的問題 – dom3k