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