2017-03-28 11 views
11

मौजूद नहीं है मुझे लगता है कि उपयोगकर्ता इनपुट द्वारा दिए गए बच्चों की संख्या में जोड़ना चाहिए parent के लिए एक कार्य के विकास व्यस्त हूँ एक माता पिता के लिए वस्तुओं बच्चों जोड़ें।केवल अगर वे अभी तक

बच्चों 3 गुण होते हैं, और उन्हें संयोजन, प्रत्येक बच्चे को हमेशा अद्वितीय होना चाहिए।

मैं Symfony और सिद्धांत और मेरी बुनियादी माता पिता वर्ग का उपयोग कर इस तरह दिखता है:

class Parent 
{ 
    /** 
    * @var Child[]|ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="Child", mappedBy="parent") 
    * @ORM\OrderBy({"dateCreated": "DESC"}) 
    */ 
    private $childs; 

    /** 
    * Add child 
    * 
    * @param \AppBundle\Entity\Child $child 
    * 
    * @return Parent 
    */ 
    public function addChild(\AppBundle\Entity\Child $child) 
    { 
     $this->childs[] = $child; 
     $child->setParent($this); 
     return $this; 
    } 

    /** 
    * Remove child 
    * 
    * @param \AppBundle\Entity\Child $child 
    */ 
    public function removeChild(\AppBundle\Entity\Child $child) 
    { 
     $this->childs->removeElement($child); 
    } 

    /** 
    * Get childs 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getChilds() 
    { 
     return $this->childs; 
    } 
} 

इस तरह मेरे बच्चे वर्ग नज़र (फिर से वास्तव में बुनियादी):

class Child 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="cupboard", type="integer") 
    */ 
    private $cupboard; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="shelf", type="integer") 
    */ 
    private $shelf; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="item", type="integer") 
    */ 
    private $item; 

    /** 
    * @var Parent 
    * 
    * @ORM\ManyToOne(targetEntity="Parent", inversedBy="childs") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="parent_id", referencedColumnName="id") 
    * }) 
    */ 
    private $parent; 

    /** 
    * Set cupboard 
    * 
    * @param string $cupboard 
    * 
    * @return Child 
    */ 
    public function setCupboard($cupboard) 
    { 
     $this->cupboard = $cupboard; 

     return $this; 
    } 

    /** 
    * Get cupboard 
    * 
    * @return int 
    */ 
    public function getCupboard() 
    { 
     return $this->cupboard; 
    } 

    /** 
    * Set shelf 
    * 
    * @param string $shelf 
    * 
    * @return Child 
    */ 
    public function setShelf($shelf) 
    { 
     $this->shelf = $shelf; 

     return $this; 
    } 

    /** 
    * Get shelf 
    * 
    * @return int 
    */ 
    public function getShelf() 
    { 
     return $this->shelf; 
    } 

    /** 
    * Set item 
    * 
    * @param string $item 
    * 
    * @return Child 
    */ 
    public function setItem($item) 
    { 
     $this->item = $item; 

     return $this; 
    } 

    /** 
    * Get item 
    * 
    * @return int 
    */ 
    public function getItem() 
    { 
     return $this->item; 
    } 

    /** 
    * Set parent 
    * 
    * @param Parent $parent 
    * 
    * @return Child 
    */ 
    public function setParent(Parent $parent) 
    { 
     $this->parent = $parent; 

     return $this; 
    } 

    /** 
    * Get parent 
    * 
    * @return Parent 
    */ 
    public function getParent() 
    { 
     return $this->parent; 
    } 
} 

तो मैं एक है कार्रवाई प्रति parent ऑब्जेक्ट (एक एक्शन एक्शन के समान) जिसे इसके लिए बच्चों को बनाना है। जब मैं लिंक (विशिष्ट माता-पिता के लिए) पर क्लिक करें एक रूप से उत्पन्न होता है तीन इनपुट फ़ील्ड:

  • अलमारी
  • शेल्फ
  • मदों की संख्या

उपयोगकर्ता तो निर्दिष्ट करने के लिए है पूर्णांक के साथ वह कितने सामान को अलमारी और क्या शेल्फ में जोड़ना चाहता है। यदि उस दिए गए अलमारी पर दिए गए अलमारी में आइटम (पूर्णांक) पहले से मौजूद है, तो इसे फिर से नहीं बनाना चाहिए, लेकिन पहले अगले उपलब्ध पूर्णांक का उपयोग आइटम के लिए किया जाना चाहिए।

मैं इसे यथासंभव सरल कैसे बना सकता हूं? मैं समझता हूं कि फ़ंक्शन Parent वर्ग से फ़ंक्शन का उपयोग कर सकता हूं, लेकिन मुझे यकीन नहीं है कि कैसे।

क्या मैं अब तक की कोशिश की है अलमारी और शेल्फ और उसके बाद यदि आइटम कि सरणी में मौजूद नहीं है यह बनाया जाना चाहिए के अनुसार एक सरणी और समूह सभी आइटम बनाने के लिए है।

यह मेरा कोड है:

public function addItemAction(Request $request, $id = null){ 
    $parent = $this->admin->getSubject(); 

    if (!$parent) { 
     throw new NotFoundHttpException(sprintf('Unable to find the object with id: %s', $id)); 
    } 

    $em = $this->getDoctrine()->getManager(); 

    $form = $this->createForm(AddItemType::class); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $kids = $popUnit->getChilds(); 
     $formParams = $request->request->get('add_item'); 

     $units = array(); 
     foreach ($kids as $kid) { 
      $units[$kid->getCupboard()][$kid->getShelf()][$kid->getItem()] = $kid->getItem(); 
     } 

     $givenShelf = $units[$formParams['cupboard']][$formParams['shelf']]; 

     for ($i = 1; $i <= $formParams['itemAmount']; $i++) { 
      if (!in_array($i, $givenShelf)) { 
       $child = new Child(); 
       $child->setParent($parent); 
       $child->setCupboard($formParams['cupboard']); 
       $child->setShelf($formParams['shelf']); 
       $child->setItem($i); 

       $em->persist($child); 
       $em->flush(); 
      } 
     } 
     return new RedirectResponse($this->admin->generateUrl('show', array('id' => $parent->getId()))); 
    } 

    return $this->render('AppBundle:Parent:add_childs.html.twig', array(
     'form' => $form->createView(), 
     'parent' =>$parent, 
    )); 
} 

अतिरिक्त जानकारी के लिए, यह कैसे मेरी प्रपत्र बिल्डर लग रहा है:

public function buildForm(FormBuilderInterface $builder, array $options) { 
    $builder 
     ->add('cupboard', NumberType::class) 
     ->add('shelf', NumberType::class) 
     ->add('itemAmount', NumberType::class, array('label' => 'Number of Items')) 
    ; 
} 

कैसे मैं इस तथ्य बनाने के लिए के साथ इस कार्रवाई के रूप में संभव के रूप में सरल बना सकते हैं सुनिश्चित करें कि माता-पिता के लिए एक अलमारी में शेल्फ में केवल अद्वितीय आइटम जोड़े गए हैं। मैं गुणों और न ही कक्षाओं को बदल सकता हूं। मुझे इसके साथ काम करना है। मैं किसी भी अन्य जटिल तरीके का उपयोग नहीं करना चाहता जैसे कि श्रोताओं तक कोई श्रोताओं या अन्य बनाना।

मुझे आशा है कि मैं मेरी स्थिति अच्छी तरह से समझाया है और मुझे आशा है कि किसी ने मुझसे कुछ अच्छी प्रतिक्रिया के साथ कर सकते हैं।

उत्तर

4

आप सही आप इस तरह Parent वर्ग के addChild समारोह को संशोधित कर सकते हैं:

/** 
    * Add child 
    * 
    * @param \AppBundle\Entity\Child $child 
    * 
    * @return Parent 
    */ 
    public function addChild(Child $child) 
    { 
     if (! is_array($this->childs) || ! in_array($child, $this->childs)) { 
      $this->childs[] = $child; 
      $child->setParent($this); 
     } 
     return $this; 
    } 

यह सशर्त बस कहा गया है कि अगर कोई बच्चों अभी तक कर रहे हैं, बच्चे को जोड़ सकते हैं या यदि बच्चे को पहले से ही नहीं है $this->childs सरणी में इसे जोड़ें।

+1

मेरे ज्ञान in_array फ़ंक्शन ऑब्जेक्ट की तुलना नहीं कर सकता – michelek

1

क्योंकि आप एक सिद्धांत के रूप में संग्रह removeChild के माध्यम से $childs के लिए पहुँच:

/** 
* Remove child 
* 
* @param \AppBundle\Entity\Child $child 
*/ 
public function removeChild(\AppBundle\Entity\Child $child) 
{ 
    $this->childs->removeElement($child); 
} 

तो, निर्माता में, आप अपने बच्चे के संग्रह पहले से प्रारंभ करना चाहिए।

public function __construct() 
{ 
    $this->childs = new ArrayCollection(); 
} 

डुप्लिकेट बच्चों से बचने के लिए, आपको संग्रह में जोड़ने से पहले मौजूदा जांचना होगा।

/** 
* Add child 
* 
* @param \AppBundle\Entity\Child $child 
* 
* @return Parent 
*/ 
public function addChild(\AppBundle\Entity\Child $child) 
{ 
    if (!$this->childs->contains($child)) { 
     $this->childs->add($child); 
     $child->setParent($this); 
    } 

    return $this; 
} 
संबंधित मुद्दे