2014-10-08 80 views
2

我有一個單向許多人徽章和要求之間有很多關聯,像這樣:如何刪除單向多對多關聯與學說2

徽章:

<?php 

namespace Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Device 
* 
* @ORM\Table(name="badges") 
* @ORM\Entity 
*/ 
class Badges 
{ 
    /** 
    * Unidirectional - Many users have Many favorite comments (OWNING SIDE) 
    * 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="Entity\Request", cascade={"persist"}) 
    */ 
    private $invite; 

} 

沒有對請求實體特殊,因爲這是一個簡單的單向關聯

添加關聯是好的。

但是,消除關聯,當我這樣做:

$em = $this->CI->doctrine->em; 

    //Get badges for new notifs 
    $badges = $user->getBadges(); 

    if($badges) 
    { 
     $invites = $badges->getInvite(); 
     if ($invites) 
     { 
      foreach ($invites as $key => $invite) 
      { 
       $badges->removeInvite($invite); 
      } 
     } 

     $em->persist($badges); 
     $em->flush(); 
    } 
    else 
    { 
     return false; 
    } 

但它不工作,每個邀請我試圖從徽章分離帶來此錯誤消息:

甲PHP錯誤遇到

嚴重性:注意

消息:未定義在DEX: 000000005ede1b52000000009e09e897

文件名: ORM/UnitOfWork.php

行號:2739

甲PHP錯誤遇到

嚴重性:警告

消息:array_pop()預計參數1 爲數組,空給出

文件名: Persisters/ManyToManyPersister.php

行號:143

現在,如果我僅僅從徽章刪除一個單一的請求:

  $invites = $badges->getInvite(); 
      if ($invites) 
      { 
       foreach ($invites as $key => $invite) 
       { 
        if(!empty($invite)) { 
         $badges->removeInvite($invite); 
         $em->persist($invite); 
         $em->persist($badges); 
         break; 
        } 
       } 
      } 

我得到:

<b>Fatal error</b>: Uncaught exception 'Doctrine\ORM\ORMInvalidArgumentException' with message 'A new entity was found through the relationship 'Entity\Badges#invite' that was not configured to cascade persist operations for entity: Entity\[email protected] To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={&quot;persist&quot;}). If you cannot find out which entity causes the problem implement 'Entity\Request#__toString()' to get a clue.' in /var/www/meetmyfriends-dev/application/libraries/Doctrine/ORM/ORMInvalidArgumentException.php:59

注意按規定,我堅持要求在此錯誤消息(實體\徽章#邀請是一個實體\請求)

這是怎麼發生的? 我應該怎麼做才能修復它?我只是想從徽章分離邀請,以去除關聯。

感謝

編輯: 這是徽章#removeInvite方法&徽章#addInvite方法實現:

/** 
    * Add invite 
    * 
    * @param \Entity\Request $invite 
    * @return Badges 
    */ 
    public function addInvite(\Entity\Request $invite) 
    { 
     $this->invite[] = $invite; 

     return $this; 
    } 

    /** 
    * Remove invite 
    * 
    * @param \Entity\Request $invite 
    */ 
    public function removeInvite(\Entity\Request $invite) 
    { 
     $this->invite->removeElement($invite); 
    } 
+0

你能告訴執行你的徽章#removeInvite方法?或者這是由教條自動創建的? – 2014-10-08 12:31:31

+0

感謝Roel,這已經自動生成了教條2,但讓我把它提起 – 2014-10-08 12:32:09

+0

我不能在Doctrine ORM v2.4.5中重現這一點。你使用的是什麼版本的Doctrine ORM? – 2014-10-08 13:12:08

回答

0

試試這個:

foreach ($invites as $key => $invite) 
      { 
       if(!empty($invite)){ 
        $badges->removeInvite($invite); 
       } 
      } 
+0

不工作不幸:/ – 2014-10-08 14:11:34

+0

刪除cascade = {「persist」}並檢查一次。 – 2014-10-10 07:40:51

+0

謝謝Nilesh,但是我在幾次測試後添加了紅色文檔時需要它。它不起作用,或者我沒有繼續下去。 – 2014-10-10 13:05:03