/** * Saves value in data object. * * Called by the render when doing $render->getValues() * Uses the group parameter to decide where to store data. * * @param Zikula_Form_View $view Reference to Form render object. * @param array &$data Data object. * * @return void */ function saveValue(Zikula_Form_View $view, &$data) { if ($this->enableDBUtil && $this->dataBased) { if ($this->group == null) { $data['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue(); } else { if (!array_key_exists($this->group, $data)) { $data[$this->group] = array(); } $data[$this->group]['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue(); } } else { if ($this->enableDoctrine && $this->dataBased) { if ($this->group == null) { $data['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField); } else { if (!array_key_exists($this->group, $data)) { $data[$this->group] = array(); } $data[$this->group]['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField); } } else { if ($this->doctrine2) { $entity = $view->get_template_vars($this->group); // load category from db $em = ServiceUtil::get('doctrine')->getEntityManager(); $category = $em->find('Zikula_Doctrine2_Entity_Category', $this->getSelectedValue()); $collection = $em->getClassMetadata(get_class($entity))->getFieldValue($entity, $this->dataField); if (!$collection) { $collection = new \Doctrine\Common\Collections\ArrayCollection(); $em->getClassMetadata(get_class($entity))->setFieldValue($entity, $this->dataField, $collection); } if ($collection->containsKey($this->registryId)) { $collection->get($this->registryId)->setCategory($category); } else { $class = $em->getClassMetadata(get_class($entity))->getAssociationTargetClass($this->dataField); $collection->set($this->registryId, new $class($this->registryId, $category, $entity)); } } else { parent::saveValue($view, $data); } } } }
/** * Saves value in data object. * * Called by the render when doing $render->getValues() * Uses the group parameter to decide where to store data. * * @param Zikula_Form_View $view Reference to Form render object. * @param array &$data Data object. * * @return void */ public function saveValue(Zikula_Form_View $view, &$data) { if ($this->enableDBUtil && $this->dataBased) { if ($this->group == null) { $data['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue(); } else { if (!array_key_exists($this->group, $data)) { $data[$this->group] = array(); } $data[$this->group]['__CATEGORIES__'][$this->dataField] = $this->getSelectedValue(); } } else { if ($this->enableDoctrine && $this->dataBased) { if ($this->group == null) { $data['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField); } else { if (!array_key_exists($this->group, $data)) { $data[$this->group] = array(); } $data[$this->group]['Categories'][$this->dataField] = array('category_id' => $this->getSelectedValue(), 'reg_property' => $this->dataField); } } else { if ($this->doctrine2) { $entity = $view->get_template_vars($this->group); // load category from db $em = ServiceUtil::getService('doctrine.entitymanager'); $collection = $em->getClassMetadata(get_class($entity))->getFieldValue($entity, $this->dataField); if (!$collection) { $collection = new \Doctrine\Common\Collections\ArrayCollection(); $em->getClassMetadata(get_class($entity))->setFieldValue($entity, $this->dataField, $collection); } if (is_array($this->getSelectedValue())) { $selectedValues = $this->getSelectedValue(); } else { $selectedValues[] = $this->getSelectedValue(); } $selectedValues = array_combine($selectedValues, $selectedValues); foreach ($collection->getKeys() as $key) { $entityCategory = $collection->get($key); if ($entityCategory->getCategoryRegistryId() == $this->registryId) { $categoryId = $entityCategory->getCategory()->getId(); if (isset($selectedValues[$categoryId])) { unset($selectedValues[$categoryId]); } else { $collection->remove($key); } } } // we do NOT flush here, as the calling module is responsible for that (Guite) //$em->flush(); foreach ($selectedValues as $selectedValue) { $category = $em->find('Zikula_Doctrine2_Entity_Category', $selectedValue); $class = $em->getClassMetadata(get_class($entity))->getAssociationTargetClass($this->dataField); $collection->add(new $class($this->registryId, $category, $entity)); } } else { parent::saveValue($view, $data); } } } }