/** * @test */ public function testProcess() { $this->doctrineRegistry->expects($this->any())->method('getManager')->will($this->returnValue($this->manager)); $this->manager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($this->queryBuilder))->with($this->equalTo('a')); $this->grid->expects($this->once())->method('getParameters')->will($this->returnValue($this->parameterBag)); $this->parameterBag->expects($this->once())->method('get')->will($this->returnValue(1)); $this->grid->expects($this->once())->method('setDatasource'); $this->combinedAuditDatasource->process($this->grid, []); }
/** * {@inheritdoc} */ public function guessColumnOptions($columnName, $entityName, $column) { $metadata = $this->entityManager->getClassMetadata($entityName); $result = []; if ($metadata->hasField($columnName) && !$metadata->hasAssociation($columnName)) { $result[Configuration::BASE_CONFIG_KEY] = ['enable' => true]; } return $result; }
/** * {@inheritdoc} */ public function afterProcess($entity) { $owner = $entity->getOwner(); $owner->setUpdatedAt(new \DateTime('now', new \DateTimeZone('UTC'))); $changeSet = $this->getChangeSet($owner); $this->entityManager->persist($owner); $this->entityManager->flush(); return $changeSet; }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $vars = array('configs' => $options['configs']); if ($form->getData()) { $fieldConfig = $this->entityManager->getExtendManager()->getConfigProvider()->getConfig($form->getParent()->getData(), $form->getName()); $fieldName = $fieldConfig->get('target_field'); $vars['attr'] = array('data-entities' => json_encode(array(array($fieldName => $form->getData()->{'get' . ucfirst($fieldName)}())))); } $view->vars = array_replace_recursive($view->vars, $vars); }
/** * Get Entity Identifier By a class name * * @param $className * @return string */ protected function getEntityIdentifier($className) { // Extend entity always have "id" identifier if (strpos($className, self::ENTITY) !== false) { return 'id'; } return $this->em->getClassMetadata($className)->getSingleIdentifierColumnName(); }
/** * @param string $entityClassName * @param array $excludedIds * @param integer $offset * * @return array */ protected function getRecordsToReset($entityClassName, array $excludedIds, $offset) { $qb = $this->em->getRepository($entityClassName)->createQueryBuilder('e'); if ($excludedIds) { $qb->andWhere($qb->expr()->notIn('e.id', $excludedIds)); } return $qb->setMaxResults(static::BATCH_SIZE)->setFirstResult($offset)->getQuery()->getResult(); }
/** * Search and return entities * * @param $search * @param $targetField * @return array */ protected function searchEntities($search, $targetField) { /** @var QueryBuilder $queryBuilder */ $queryBuilder = $this->entityManager->getRepository($this->entityName)->createQueryBuilder('e'); if ($this->isCustomField) { $targetField = ExtendConfigDumper::FIELD_PREFIX . $targetField; } $queryBuilder->where($queryBuilder->expr()->like('e.' . $targetField, $queryBuilder->expr()->literal($search . '%'))); return $queryBuilder->getQuery()->getArrayResult(); }
/** * @param string $entity * @param string $keyField * @param string $labelField * * @return array */ protected function getChoices($entity, $keyField, $labelField) { $queryBuilder = $this->entityManager->getRepository($entity)->createQueryBuilder('e'); //select only id and label fields $queryBuilder->select("e.{$keyField}, e.{$labelField}"); $result = $this->aclHelper->apply($queryBuilder)->getResult(); $choices = []; foreach ($result as $item) { $choices[$item[$keyField]] = $item[$labelField]; } return $choices; }
public function testSetGetFiltersCollection() { $this->assertInstanceOf('Oro\\Bundle\\EntityBundle\\ORM\\Query\\FilterCollection', $this->manager->getFilters()); $this->manager->setFilterCollection($this->filterCollection); $this->assertAttributeEquals($this->filterCollection, 'filterCollection', $this->manager); $this->assertEquals($this->filterCollection, $this->manager->getFilters()); $this->assertFalse($this->manager->hasFilters()); $this->filterCollection->expects($this->once())->method('getEnabledFilters')->will($this->returnValue(true)); $this->assertTrue($this->manager->hasFilters()); $this->filterCollection->expects($this->at(0))->method('isClean')->will($this->returnValue(true)); $this->filterCollection->expects($this->at(1))->method('isClean')->will($this->returnValue(false)); $this->assertTrue($this->manager->isFiltersStateClean()); $this->assertFalse($this->manager->isFiltersStateClean()); }
/** * Get list of Diamante tables to purge data from * * @return array */ protected function getTablesList() { $entitiesMetadata = array($this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\TicketHistory::getClassName()), $this->entityManager->getClassMetadata('\\Oro\\Bundle\\TagBundle\\Entity\\Tag'), $this->entityManager->getClassMetadata('\\Oro\\Bundle\\TagBundle\\Entity\\Tagging')); $toPurge = array(); $quoteStrategy = new DefaultQuoteStrategy(); /** @var $entity \Doctrine\ORM\Mapping\ClassMetadata */ foreach ($entitiesMetadata as $entity) { $tableName = $entity->getTableName(); $toPurge[] = $tableName; foreach ($entity->getAssociationMappings() as $assoc) { if (isset($assoc['joinTable'])) { $toPurge[] = $quoteStrategy->getJoinTableName($assoc, $entity, $this->dbPlatform); } } } return $toPurge; }
/** * @param FormEvent $event */ public function postSubmit(FormEvent $event) { $form = $event->getForm(); $configModel = $form->getConfig()->getOption('config_model'); $data = $event->getData(); $labelsToBeUpdated = []; foreach ($this->configManager->getProviders() as $provider) { $scope = $provider->getScope(); if (isset($data[$scope])) { $configId = $this->configManager->getConfigIdByModel($configModel, $scope); $config = $provider->getConfigById($configId); $translatable = $provider->getPropertyConfig()->getTranslatableValues($configId); foreach ($data[$scope] as $code => $value) { if (in_array($code, $translatable)) { // check if a label text was changed $labelKey = $config->get($code); if (!$configModel->getId()) { $labelsToBeUpdated[$labelKey] = $value; } elseif ($value != $this->translator->trans($labelKey)) { $labelsToBeUpdated[$labelKey] = $value; } // replace label text with label name in $value variable $value = $config->get($code); } $config->set($code, $value); } $this->configManager->persist($config); } } if ($form->isValid()) { // update changed labels if any if (!empty($labelsToBeUpdated)) { $locale = $this->translator->getLocale(); foreach ($labelsToBeUpdated as $labelKey => $labelText) { // save into translation table /** @var TranslationRepository $translationRepo */ $translationRepo = $this->em->getRepository(Translation::ENTITY_NAME); $translationRepo->saveValue($labelKey, $labelText, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI); } // mark translation cache dirty $this->dbTranslationMetadataCache->updateTimestamp($locale); } $this->configManager->flush(); } }
/** * Perform the actual purge by truncating each of tables from the provided list * Throws an Exception (in case if anything goes wrong) which stops the process of truncating to prevent * the possible damage to DB * * @param array $tablesList * @throws \Exception */ protected function purgeTables(array $tablesList) { $connection = $this->entityManager->getConnection(); $dbPlatform = $connection->getDatabasePlatform(); $connection->query('SET FOREIGN_KEY_CHECKS=0;'); foreach ($tablesList as $table) { $query = $dbPlatform->getTruncateTableSql($table); try { $this->output->writeln('Purging data from ' . $table); $connection->executeUpdate($query); } catch (\Exception $e) { $this->output->writeln('Error purging data from \'' . $table . '\'. Error: ' . $e->getMessage()); $connection->query('SET FOREIGN_KEY_CHECKS=1;'); throw $e; } } $connection->query('SET FOREIGN_KEY_CHECKS=1;'); }
public function preSubmitData(FormEvent $event) { list($entityId, $model) = $this->prepareEvent($event); $saved = []; if ($entityId) { $saved = $this->relations->findByFieldId($model->getId(), $entityId); array_walk($saved, function (&$item) { $item = $item->getOption()->getId(); }); } $data = $event->getData(); if (empty($data)) { $data = []; } if (!is_array($data)) { $data = [$data]; } if ($entityId) { /** * Save selected options */ $toSave = array_intersect($data, $saved); foreach ($data as $option) { if (!in_array($option, $saved)) { $optionRelation = new OptionSetRelation(); $optionRelation->setData(null, $entityId, $model, $this->options->find($option)); $toSave[] = $option; $this->em->persist($optionRelation); } } /** * Remove unselected */ if ($entityId && $this->relations->count($model->getId(), $entityId)) { $toRemove = $this->relations->findByNotIn($model->getId(), $entityId, $toSave); foreach ($toRemove as $option) { $this->em->remove($option); } } } }
/** * @param string $targetClass * @param FieldConfigId $fieldId */ protected function updateRelationValues($targetClass, FieldConfigId $fieldId) { $extendProvider = $this->em->getExtendConfigProvider(); $targetConfig = $extendProvider->getConfig($targetClass); $relations = $targetConfig->get('relation', false, []); $schema = $targetConfig->get('schema', false, []); foreach ($relations as &$relation) { if ($relation['target_field_id'] == $fieldId) { if ($relation['owner']) { $relation['assign'] = true; } /** @var FieldConfigId $relationFieldId */ $relationFieldId = $relation['field_id']; if ($relationFieldId) { $schema['relation'][$relationFieldId->getFieldName()] = $relationFieldId->getFieldName(); } } } $targetConfig->set('relation', $relations); $targetConfig->set('schema', $schema); $extendProvider->persist($targetConfig); }
/** * PRE_SUBMIT event handler * * @param FormEvent $event */ public function preSubmitData(FormEvent $event) { $entityId = $this->getEntityId($event); if ($entityId) { $configFieldModel = $this->configManager->getConfigFieldModel($event->getForm()->getConfig()->getOption('entityClassName'), $event->getForm()->getConfig()->getOption('entityFieldName')); $savedOptionIds = $this->getSavedOptionIds($configFieldModel, $entityId); $data = $event->getData(); if (empty($data)) { $data = []; } if (!is_array($data)) { $data = [$data]; } /** * Save selected options */ $toSave = array_intersect($data, $savedOptionIds); foreach ($data as $option) { if (!in_array($option, $savedOptionIds)) { $optionRelation = new OptionSetRelation(); $optionRelation->setData(null, $entityId, $configFieldModel, $this->options->find($option)); $toSave[] = $option; $this->em->persist($optionRelation); } } /** * Remove unselected */ if ($entityId && $this->relations->count($configFieldModel->getId(), $entityId)) { $toRemove = $this->relations->findByNotIn($configFieldModel->getId(), $entityId, $toSave); foreach ($toRemove as $option) { $this->em->remove($option); } } } }
/** * @param string $entityClassName * @param array $ids * @param integer $offset * * @return array */ protected function getRecordsToRecalculate($entityClassName, $ids, $offset) { $entityRepository = $this->em->getRepository($entityClassName); return $entityRepository->findBy(['id' => $ids], ['id' => 'ASC'], self::BATCH_SIZE, $offset); }
/** * @return EmailOrigin[] */ protected function getOrigins() { $criteria = ['owner' => $this->securityContext->getToken()->getUser(), 'isActive' => true]; return $this->em->getRepository(self::EMAIL_ORIGIN)->findBy($criteria); }