2013-04-19 4 views
9

एक Zend फ्रेमवर्क 2 परियोजना में, हम दो सिद्धांत 2 संस्थाओं मिला है और हम एक संग्रह allready डेटाबेस में बचाया से एक तत्व को दूर करने के ...OneToMany पर ArrayCollection से तत्व निकालें इसे लगातार() पर हटाएं?

तो हम एक पहले FormGroupConstraint वर्णित निकाय है चाहते हैं:

/** 
* FormGroupConstraint 
* 
* @ORM\Table(name="form_group_constraint") 
* @ORM\Entity(repositoryClass="Application\Dao\FormGroupConstraintDao") 
*/ 
class FormGroupConstraint { 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    protected $id; 

    /** 
    * @param \Doctrine\Common\Collections\ArrayCollection 
    * @ORM\OneToMany(targetEntity="Application\Entity\FormQuestionConstraint", mappedBy="groupConstraint", fetch="EAGER", cascade={"persist", "merge", "refresh", "remove"}) 
    */ 
    protected $constraints; 

    public function __construct() 
      $this->constraints = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * @param \Doctrine\Common\Collections\ArrayCollection $constraints 
    */ 
    public function addConstraints($constraints) { 
     foreach ($constraints as $constraint) { 
      $this->constraints->add($constraint); 
     } 
     return $this->constraints; 
    } 
    /** 
    * @param \Doctrine\Common\Collections\ArrayCollection $constraints 
    */ 
    public function removeConstraints($constraintsToRemove) { 
      foreach ($constraintsToRemove as $key => $constraintToRemove) { 
       $this->constraints->removeElement($constraintToRemove); 
      } 
      return $this->constraints; 
    } 
} 

और उप FormQuestionConstraint वर्णित निकाय:

/** 
* FormQuestionConstraint 
* 
* @ORM\Table(name="form_question_constraint") 
* @ORM\Entity(repositoryClass="Application\Dao\FormQuestionConstraintDao") 
*/ 
class FormQuestionConstraint 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    protected $id; 

    /** 
    * @var \Application\Entity\FormGroupConstraint 
    * 
    * @ORM\ManyToOne(targetEntity="Application\Entity\FormGroupConstraint", cascade= {"persist", "merge", "refresh", "remove"}) 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="form_group_constraint_id", referencedColumnName="id") 
    * }) 
    */ 
    protected $groupConstraint; 
} 

तो अगर हम जारी रहती है, बनाने के लिए, एक FormGroupConstraint इकाई, कोई समस्या नहीं, फ्लश की कोशिश लेकिन हम सिद्धांत-ORM उपयोग कर रहे हैं जब हम $ बाधाओं ArrayCollection, कुछ नहीं होता का एक तत्व को दूर करने के ...

चाहते Zend 2 देव-गुरु में composer.phar के साथ स्थापित करने के लिए मॉड्यूल ...

यहाँ हम क्या करने की कोशिश कर रहे हैं का एक उदाहरण है:

$constraint = $this->findConstraintByGroup(1); 

$formGroupConstraint = $this->_em->findOneById(1); 

$formGroupConstraint->getConstraints()->removeElement($constraint); 
$this->_em->persist($formGroupConstraint); 
$this->_em->flush(); 

कोई त्रुटि है, लेकिन कोई हटाने या निकालने के लिए ... और यदि हम) var_dump() getConstraints (से पहले (जारी रहती है), वास्तव में तत्व ArrayCollection में अब भी है ...

किसी को भी हमें यह बताएं कि हम कर सकते थे किया जा सका ऐसा करें या तत्व को क्यों हटाया नहीं गया है?

$this->_em->remove($constraint); 
$this->_em->flush(); 

उत्तर

3

क्योंकि संदर्भ FormQuestionConstraint पर आयोजित किया जाता है आपको बस इतना करना orphanRemoval=true संबंध के OneToMany पक्ष पर, उदाहरण के लिए:

@ORM\OneToMany(
    targetEntity="Application\Entity\FormQuestionConstraint", 
    mappedBy="groupConstraint", 
    cascade={"all"}, 
    orphanRemoval=true 
) 

इस व्यवहार का कारण यह है कि इकाई को ArrayCollection से हटा दिया गया है, लेकिन संग्रह की सामग्री केवल मूल इकाई को इंगित करने वाली बाल इकाई की विदेशी कुंजी द्वारा निर्धारित की जाती है।

जब सिद्धांत अगली बार माता-पिता इकाई के बच्चों को देखता है, तो यह अभी भी वहां है और अगले अनुरोध में फिर से दिखाया जाएगा, जब ArrayCollection डेटाबेस से फिर से प्राप्त किया जाता है।

यदि orphanRemovaltrue पर सेट है, तो ऐसी बाल संस्थाओं को persist() पर हटा दिया जाएगा।

+0

आपके उत्तर के लिए धन्यवाद, लेकिन समस्या यह है कि, जब हम एरिक को हटाते हैं तो देखने के लिए ऐरेकोलेक्शन पर लूप करने का प्रयास करते हैं, डेटाबेस और वर्तमान ऐरे कोलेक्शन के बीच कोई बदलाव नहीं होता है ... कोई भी निकासी ($ बाधा) बेकार है ??? यह विधि ArrayCollection पर कुछ भी क्यों नहीं करती है? (हाइबरनेट सूची या अन्य ओआरएम संग्रह कंटेनर इस तरह काम कर सकता है ...) –

+0

'' फ्लश() '' ?? कॉल करने के बाद भी ''ऐरेकोलेक्शन'' में कोई बदलाव नहीं है ?? '' $ - - _ em-> फ्लश() '' को कॉल करने का प्रयास करें। –

+0

हम क्या करना चाहते हैं यह है: ** 1: लोड $ formGroupConstraint ** ** 2: फोन करके तत्वों को दूर $ formGroupConstraint-> getConstraints() -> removeElement ($ बाधा); ** ** 3 : अंत में, यह $ - - _ em-> जारी रखें ($ formGroupConstraint); **;> _ em-> फ्लश() - इस $। लेकिन संग्रह से और न ही डेटाबेस से कुछ भी हटा दिया गया है। क्या यह प्रक्रिया संभव है? [डिक्ट्राइन एसोसिएशन डॉक्स] से –

29

हो सकता है कि थोड़ी देर हो चुकी है, लेकिन

@ORM\OneToMany(
    targetEntity="Application\Entity\FormQuestionConstraint", 
    mappedBy="groupConstraint", 
    cascade={"persist", "remove"}, 
    orphanRemoval=true 
) 
+0

यह पहले की तुलना में बेहतर समाधान है, अतिरिक्त कोड की आवश्यकता नहीं है। –

+0

अन्य उपयोगकर्ताओं के लिए वाईएएमएल के मामले में (जैसे मेरे): अनाथ रिमूवल: वनटॉनी संबंध के लिए "बाएं" फ़ील्ड में सत्य। –

+0

सिम्फनी या सिद्धांत के लिए कुछ भी ढूंढना इतना कठिन क्यों है, धन्यवाद, यह बहुत अच्छा जवाब है! –

4

आप जोड़ सकते हैं संबंध के व्युत्क्रम की ओर (OneToMany) को orphanRemoval=true जोड़ने का प्रयास करें:

संबंधित मुद्दे