Esempio n. 1
0
 /**
  * {@inheritDoc}
  */
 public function getReference($name, $property = null)
 {
     if (isset($this->objectsCache[$name])) {
         try {
             $reference = $this->entityManager->merge($this->objectsCache[$name]);
         } catch (\Exception $e) {
             var_dump($e->getMessage());
             var_dump($this->objectsCache[$name]);
             exit;
         }
     } else {
         if (isset($this->references[$name])) {
             $reference = $this->references[$name];
         } else {
             throw new \UnexpectedValueException('Reference ' . $name . ' is not defined');
         }
     }
     if ($property !== null) {
         if (property_exists($reference, $property)) {
             $prop = new \ReflectionProperty($reference, $property);
             if ($prop->isPublic()) {
                 return $reference->{$property};
             }
         }
         $getter = 'get' . ucfirst($property);
         if (method_exists($reference, $getter) && is_callable(array($reference, $getter))) {
             return $reference->{$getter}();
         }
         throw new \UnexpectedValueException('Property ' . $property . ' is not defined for reference ' . $name);
     }
     return $reference;
 }
 /**
  * @param array $countries
  */
 public function saveCountries($countries)
 {
     foreach ($countries as $country) {
         if ($this->em->find('MainBundle:Country', $country->getCodeCountry())) {
             $this->em->merge($country);
         } else {
             $this->em->persist($country);
         }
     }
     $this->em->flush();
 }
 private function prepare($data)
 {
     $metaDataClass = $this->entityManager->getClassMetadata(get_class($data));
     $assocFields = $metaDataClass->getAssociationMappings();
     foreach ($assocFields as $assoc) {
         $relatedEntities = $metaDataClass->reflFields[$assoc['fieldName']]->getValue($data);
         if ($relatedEntities instanceof Collection) {
             if ($relatedEntities === $metaDataClass->reflFields[$assoc['fieldName']]->getValue($data)) {
                 continue;
             }
             if ($relatedEntities instanceof PersistentCollection) {
                 // Unwrap so that foreach() does not initialize
                 $relatedEntities = $relatedEntities->unwrap();
             }
             foreach ($relatedEntities as $relatedEntity) {
                 $relatedEntitiesState = $this->entityManager->getUnitOfWork()->getEntityState($relatedEntities);
                 if ($relatedEntitiesState === UnitOfWork::STATE_DETACHED) {
                     $metaDataClass->setFieldValue($data, $assoc['fieldName'], $this->entityManager->merge($relatedEntity));
                 }
             }
         } else {
             if ($relatedEntities !== null) {
                 $relatedEntitiesState = $this->entityManager->getUnitOfWork()->getEntityState($relatedEntities);
                 if ($relatedEntitiesState === UnitOfWork::STATE_DETACHED) {
                     $metaDataClass->setFieldValue($data, $assoc['fieldName'], $this->entityManager->merge($relatedEntities));
                 }
             }
         }
     }
 }
 /**
  * @param array $sections
  */
 public function saveSections($sections)
 {
     foreach ($sections as $section) {
         $oldSection = $this->sectionFetcher->getSection($section->getCodeSection());
         if (!$oldSection) {
             $this->em->persist($section);
             $this->em->flush();
         } else {
             $section->setGuide($oldSection->getGuide());
             $section->setToken($oldSection->getToken());
             $section->setLogoUrl($oldSection->getLogoUrl());
             $section->setAddedAt($oldSection->getAddedAt());
             $section->setActivated($oldSection->isActivated());
             $section->setUpdatedAt();
             $this->em->merge($section);
             $this->em->flush();
         }
     }
 }
Esempio n. 5
0
 private function merge(Article $article)
 {
     $this->em->merge($article);
 }
Esempio n. 6
0
 public function mergeAndPersist(EntityManagerInterface $entityManager)
 {
     $settings = $entityManager->merge($this);
     $entityManager->persist($settings);
     return $settings;
 }
 public function mergeAndPersist(EntityManagerInterface $entityManager)
 {
     $tempOptions = array();
     foreach ($this->list->getOptions() as $option) {
         if (!$option->getSelectAttributeOptionID()) {
             $tempOptions[] = $option;
             $this->list->getOptions()->removeElement($option);
         }
     }
     $settings = $entityManager->merge($this);
     foreach ($tempOptions as $option) {
         $option->setOptionList($settings->list);
         $settings->list->getOptions()->add($option);
     }
     $entityManager->persist($settings);
     return $settings;
 }
 /**
  * @param $entity
  * @return mixed
  */
 public function delete($entity)
 {
     $entity = $this->entityManager->merge($entity);
     $this->entityManager->remove($entity);
     $this->entityManager->flush();
 }