/**
  * @param User $user
  * @param array $actualRepositories
  */
 protected function updateUserRepositories(User $user, array $actualRepositories)
 {
     // Reindex array by GH repository ids
     $actualRepositories = array_column($actualRepositories, null, 'id');
     $userRepositoryIds = [];
     /** @var Repository $repository */
     foreach ($user->getRepositories() as $repository) {
         // Repository was removed from github
         if (!array_key_exists($repository->getGithubId(), $actualRepositories)) {
             $user->getRepositories()->removeElement($repository);
             continue;
         }
         // Collect ids of user repositories
         $userRepositoryIds[] = $repository->getGithubId();
     }
     $repositoryManager = $this->doctrine->getManagerForClass(Repository::class);
     // Create repository entities for new repositories from github
     array_map(function ($repository) use($userRepositoryIds, $user, $repositoryManager) {
         if (!in_array($repository['id'], $userRepositoryIds)) {
             $repository = (new Repository())->setGithubId($repository['id'])->setFullName($repository['full_name'])->setName($repository['name'])->setOwner($user);
             $repositoryManager->persist($repository);
             $user->addRepository($repository);
         }
     }, $actualRepositories);
     $repositoryManager->flush();
 }
 /**
  * Get marketing list types choices.
  *
  * @return array
  */
 public function getListTypeChoices()
 {
     /** @var MarketingListType[] $types */
     $types = $this->registry->getManagerForClass(self::MARKETING_LIST_TYPE)->getRepository(self::MARKETING_LIST_TYPE)->findBy(array(), array('name' => 'ASC'));
     $results = array();
     foreach ($types as $type) {
         $results[$type->getName()] = $type->getLabel();
     }
     return $results;
 }
 /**
  * {@inheritdoc}
  */
 public function addManager(ManagerInterface $manager)
 {
     $class = $manager->getClass();
     $em = $this->doctrine->getManagerForClass($class);
     $manager->setObjectManager($em);
     $manager->setRepository($em->getRepository($class));
     $manager->setEventDispatcher($this->eventDispatcher);
     $manager->setFactory($this);
     $this->managers[$class] = $manager;
 }
 /**
  * {@inheritdoc}
  */
 public function getEntityManager()
 {
     if ($this->entityManager instanceof ObjectManager) {
         return $this->entityManager;
     }
     $em = $this->registry->getManagerForClass(self::TRANSACTION_CLASS);
     if (!$em) {
         throw new \RuntimeException(sprintf('No entity manager defined for class %s', self::TRANSACTION_CLASS));
     }
     return $em;
 }
Example #5
0
 /**
  * Returns query builder that allows to fetch list of lifetime values for each account
  *
  * @param null $ids     the identifiers of accounts the lifetimeValues for which is need to be fetched
  *
  * @return QueryBuilder
  */
 public function getAccountsLifetimeQueryBuilder($ids = null)
 {
     /** @var EntityManager $em */
     $em = $this->registry->getManagerForClass('OroCRMChannelBundle:LifetimeValueHistory');
     $qb = $em->createQueryBuilder();
     $qb->select('IDENTITY(h.account) AS accountId, SUM(h.amount) AS lifetimeValue')->from('OroCRMChannelBundle:LifetimeValueHistory', 'h')->leftJoin('h.dataChannel', 'ch')->andWhere('ch.status = :channelStatus')->setParameter('channelStatus', $qb->expr()->literal((int) Channel::STATUS_ACTIVE))->andWhere('h.status = :status')->setParameter('status', $qb->expr()->literal(LifetimeValueHistory::STATUS_NEW))->groupBy('h.account');
     if ($ids) {
         $qb->andWhere('IDENTITY(h.account) IN(:ids)')->setParameter('ids', array_values($ids));
     }
     return $qb;
 }
Example #6
0
 /**
  * Process form
  *
  * @param EmailCampaign $entity
  *
  * @return bool
  */
 public function process(EmailCampaign $entity)
 {
     $this->form->setData($entity);
     if (in_array($this->request->getMethod(), array('POST', 'PUT'))) {
         $this->form->submit($this->request);
         if (!$this->request->get(self::UPDATE_MARKER, false) && $this->form->isValid()) {
             $em = $this->registry->getManagerForClass('OroCRMCampaignBundle:EmailCampaign');
             $em->persist($entity);
             $em->flush();
             return true;
         }
     }
     return false;
 }
 /**
  * @param Repository $repository
  */
 public function toggleRepositoryWebhook(Repository $repository)
 {
     /** @var User $user */
     $user = $this->tokenStorage->getToken()->getUser();
     if ($repository->getEnabled()) {
         $this->githubManager->removeWebhook($user->getUsername(), $repository->getName(), $repository->getWebhookId());
         $webhookId = null;
     } else {
         $webhookId = $this->githubManager->createWebhook($user->getUsername(), $repository->getName());
     }
     // Update state of repository webhook
     $repository->setWebhookId($webhookId);
     $repositoryManager = $this->doctrine->getManagerForClass(ClassUtils::getClass($repository));
     $repositoryManager->flush();
 }
 /**
  * Finds an entity
  *
  * @param string $class
  * @param array  $data
  *
  * @return object|null
  */
 protected function findEntity($class, array $data)
 {
     $repository = $this->doctrine->getManagerForClass($class)->getRepository($class);
     $identifierProperties = $this->getEntityIdentifierProperties($repository);
     $identifier = $this->getEntityIdentifier($identifierProperties, $data);
     return $this->findOneByIdentifier($repository, $identifier);
 }
 /**
  * @param object $originEntity
  * @param array  $criteria
  * @throws \RuntimeException
  * @return object|null
  */
 protected function getEntity($originEntity, $criteria)
 {
     if (!$this->registry) {
         throw new \RuntimeException('Registry was not set');
     }
     $className = ClassUtils::getRealClass($originEntity);
     return $this->registry->getManagerForClass($className)->getRepository($className)->findOneBy($criteria);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $serializerMetadata = $this->metadataFactory->getMetadataForClass($this->class);
     $manager = $this->registry->getManagerForClass($this->class);
     $doctrineMetadata = $manager->getClassMetadata($this->class);
     foreach ($serializerMetadata->propertyMetadata as $propertyMetadata) {
         $name = $propertyMetadata->name;
         if (in_array($name, $doctrineMetadata->getIdentifierFieldNames()) && !$this->identifierOverwrite) {
             continue;
         }
         if (!in_array($this->group, $propertyMetadata->groups)) {
             continue;
         }
         $type = null;
         $nullable = true;
         if (isset($doctrineMetadata->fieldMappings[$name])) {
             $fieldMetadata = $doctrineMetadata->fieldMappings[$name];
             $type = isset($fieldMetadata['type']) ? $fieldMetadata['type'] : null;
             $nullable = isset($fieldMetadata['nullable']) ? $fieldMetadata['nullable'] : false;
         } else {
             if (isset($doctrineMetadata->associationMappings[$name])) {
                 $associationMetadata = $doctrineMetadata->associationMappings[$name];
                 if (isset($associationMetadata['joinColumns']['nullable'])) {
                     $nullable = $associationMetadata['joinColumns']['nullable'];
                 } else {
                     if (isset($associationMetadata['inverseJoinColumns']['nullable'])) {
                         $nullable = $associationMetadata['inverseJoinColumns']['nullable'];
                     }
                 }
             }
         }
         switch ($type) {
             case 'datetime':
                 $builder->add($name, $type, array('required' => !$nullable, 'widget' => 'single_text'));
                 break;
             case 'boolean':
                 $childBuilder = $builder->create($name, null, array('required' => !$nullable));
                 $childBuilder->addEventSubscriber(new FixCheckboxDataListener());
                 $builder->add($childBuilder);
                 break;
             default:
                 $builder->add($name, null, array('required' => !$nullable));
                 break;
         }
     }
 }
Example #11
0
 /**
  * @param \Class $class
  *
  * @return EntityManager
  */
 public function getEntityManager($class = null)
 {
     if (!$class) {
         return $this->_registry->getEntityManager();
     }
     if (is_object($class)) {
         $class = get_class($class);
     }
     if (!isset($this->_cache[$class])) {
         $em = $this->_registry->getManagerForClass($class);
         if (!$em) {
             throw new \RuntimeException(sprintf('No entity manager defined for class %s', $class));
         }
         $this->_cache[$class] = $em;
     }
     return $this->_cache[$class];
 }
 /**
  * Finds an entity
  *
  * @param string $class
  * @param array  $data
  *
  * @throws \LogicException
  *
  * @return object|null
  */
 protected function findEntity($class, array $data)
 {
     $repository = $this->doctrine->getManagerForClass($class)->getRepository($class);
     if ($repository instanceof IdentifiableObjectRepositoryInterface) {
         $identifierProperties = $repository->getIdentifierProperties();
         $identifier = $this->getEntityIdentifier($identifierProperties, $data);
         return $repository->findOneByIdentifier($identifier);
     }
     return null;
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function getIdentifyingValue($object)
 {
     $className = get_class($object);
     $em = $this->registry->getManagerForClass($className);
     $metadata = $em->getClassMetadata($className);
     $identifier = $metadata->getIdentifier();
     if (count($identifier) > 1) {
         throw new CompositePrimaryKeyException(sprintf('Composite Primary Keys cannot be resolved to a single scalar value on Entity: %s', get_class($object)));
     }
     try {
         $pkValue = $this->propertyAccessor->getValue($object, $identifier[0]);
     } catch (AccessException $e) {
         throw new IdentifyingFieldNotReachableException(sprintf('The property accessor was unable to access %s.', $identifier[0]));
     }
     if (is_object($pkValue)) {
         $pkValue = $this->getIdentifyingValue($pkValue);
     }
     return $pkValue;
 }
Example #14
0
 /**
  * Returns query builder that allows to fetch account lifetime value from history table
  * Following parameters are required to be passed:
  *  - account  Account entity or identifier
  *
  * Following parameters are optional:
  *  - dataChannel - Channel entity or id to be used for fetch criteria, required if $addChannelParam is set to true
  *
  * @param bool $addChannelParam
  *
  * @return QueryBuilder
  */
 public function getChannelAccountLifetimeQueryBuilder($addChannelParam = false)
 {
     /** @var EntityManager $em */
     $em = $this->registry->getManagerForClass('OroCRMChannelBundle:LifetimeValueHistory');
     $qb = $em->createQueryBuilder();
     $qb->from('OroCRMChannelBundle:LifetimeValueHistory', 'h');
     $qb->select('SUM(h.amount)');
     $qb->andWhere('h.account = :account');
     if ($addChannelParam) {
         // do not change order, need for idx
         $qb->andWhere('h.dataChannel = :dataChannel');
     }
     $qb->leftJoin('h.dataChannel', 'ch');
     $qb->andWhere('ch.status = :channelStatus');
     $qb->setParameter('channelStatus', $qb->expr()->literal((int) Channel::STATUS_ACTIVE));
     $qb->andWhere('h.status = :status');
     $qb->setParameter('status', $qb->expr()->literal(LifetimeValueHistory::STATUS_NEW));
     $qb->setMaxResults(1);
     return $qb;
 }
 /**
  * Finds an entity
  *
  * @param string $class
  * @param array  $data
  *
  * @return object|null
  */
 protected function findEntity($class, array $data)
 {
     $repository = $this->doctrine->getManagerForClass($class)->getRepository($class);
     if ($repository instanceof ReferableEntityRepositoryInterface) {
         $reference = implode('.', array_map(function ($property) use($class, $data) {
             if (!isset($data[$property])) {
                 throw new MissingIdentifierException();
             }
             return $data[$property];
         }, $repository->getReferenceProperties()));
         return $repository->findByReference($reference);
     }
 }
 /**
  * Outputs serialized entities
  *
  * @param Request $request
  */
 protected function export(Request $request)
 {
     $iterator = $this->queryGenerator->createQueryBuilder($request, $this->configuration->getName())->getQuery()->iterate();
     $headersSent = false;
     $manager = $this->doctrine->getManagerForClass($this->configuration->getEntityClass());
     foreach ($iterator as $index => $item) {
         if (!count($item[0])) {
             continue;
         }
         $norm = $this->serializer->normalize($item[0], $this->options['serializer_format'], $this->options['serializer_context']);
         if (!$headersSent) {
             echo $this->serializer->encode(array_keys($norm), $this->options['serializer_format'], $this->options['serializer_context']);
             $headersSent = true;
         }
         echo $this->serializer->encode($norm, $this->options['serializer_format'], $this->options['serializer_context']);
         flush();
         if (0 === ($index + 100) % $this->options['batch_size']) {
             $manager->clear();
         }
     }
 }
 /**
  * @param FormEvent $event
  */
 public function postSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     $parentForm = $form->getParent();
     $parentData = $parentForm->getData();
     $propertyAccessor = PropertyAccess::createPropertyAccessor();
     if ($parentData) {
         $entityClass = get_class($parentData);
         $em = $this->doctrine->getManagerForClass($entityClass);
         $translatableFields = $this->getTranslatableFields($entityClass);
         $data = $event->getData();
         $locales = $this->localeList;
         $reflectionClass = new \ReflectionClass($entityClass);
         $gedmoAnnotations = $this->isPersonnalTranslationRecursive($reflectionClass);
         if ($gedmoAnnotations && is_object($gedmoAnnotations) && $gedmoAnnotations->class != '') {
             $repository = $this->translationRepository;
         } else {
             $repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
         }
         foreach ($locales as $locale => $localeConf) {
             foreach ($translatableFields as $field => $type) {
                 if ($parentForm->has($field)) {
                     $fieldData = '';
                     $localeFieldName = sprintf('%s-%s', $field, $locale);
                     if (isset($data[$localeFieldName])) {
                         $fieldData = $data[$localeFieldName];
                     } elseif (isset($data[$field])) {
                         $fieldData = $data[$field];
                     }
                     if ($field != 'slug' || $fieldData) {
                         if ($repository instanceof BigfootTranslationRepository && $this->currentLocale == $locale) {
                             $fieldData = $propertyAccessor->getValue($parentData, $field);
                         }
                         $repository->translate($parentData, $field, $locale, $fieldData);
                     }
                 }
             }
         }
     }
 }
 /**
  * @return RepositoryRepository
  */
 protected function getRepositoryRepository()
 {
     return $this->doctrine->getManagerForClass('ApproveCodeUserBundle:Repository')->getRepository('ApproveCodeUserBundle:Repository');
 }
 public function __construct(RegistryInterface $registry, $class)
 {
     $this->class = $class;
     $this->entity_manager = $registry->getManagerForClass($class);
     $this->entity_repository = $this->entity_manager->getRepository($class);
 }
 private function getEntityManager()
 {
     return $this->doctrine->getManagerForClass($this->options['class']);
 }
 /**
  * @return \Doctrine\ORM\EntityManager
  */
 private function getEntityManager()
 {
     return $this->registry->getManagerForClass($this->blockManager->getClass());
 }