Example #1
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var ConfigContext $context */
     /** @var array $definition */
     $definition = $context->getResult();
     if (empty($definition) || !array_key_exists(ConfigUtil::FIELDS, $definition)) {
         // virtual fields is added only if a definition of fields exists
         return;
     }
     $entityClass = $context->getClassName();
     $virtualFields = $this->virtualFieldProvider->getVirtualFields($entityClass);
     if (!empty($virtualFields)) {
         foreach ($virtualFields as $field) {
             $query = $this->virtualFieldProvider->getVirtualFieldQuery($entityClass, $field);
             $propertyPath = $this->getPropertyPath($query);
             if (!empty($propertyPath)) {
                 $definition[ConfigUtil::FIELDS][$field][ConfigUtil::PROPERTY_PATH] = $propertyPath;
                 if (!empty($query['select']['label']) && $context->hasExtra(DescriptionsConfigExtra::NAME)) {
                     $definition[ConfigUtil::FIELDS][$field][ConfigUtil::LABEL] = new Label($query['select']['label']);
                 }
             }
         }
         $context->setResult($definition);
     }
 }
 /**
  * @param string $entityClass
  * @param string $fieldName
  * @param string $alias
  * @param array $fieldConfig
  * @param array $joins
  * @param string $expected
  *
  * @dataProvider virtualFieldsProvider
  */
 public function testGetFieldExprVirtual($entityClass, $fieldName, $alias, array $fieldConfig, array $joins, $expected)
 {
     $from = $this->getMockBuilder('Doctrine\\ORM\\Query\\Expr\\From')->disableOriginalConstructor()->getMock();
     $from->expects($this->atLeastOnce())->method('getAlias')->will($this->returnValue($alias));
     /** @var \PHPUnit_Framework_MockObject_MockObject|QueryBuilder $qb */
     $qb = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $qb->expects($this->atLeastOnce())->method('getDQLPart')->will($this->returnValueMap([['from', [$from]], ['join', $joins]]));
     $this->virtualFieldProvider->expects($this->once())->method('isVirtualField')->with($entityClass, $fieldName)->will($this->returnValue(true));
     $this->virtualFieldProvider->expects($this->once())->method('getVirtualFieldQuery')->with($entityClass, $fieldName)->will($this->returnValue($fieldConfig));
     $this->assertEquals($expected, $this->helper->getFieldExpr($entityClass, $qb, $fieldName));
 }
 /**
  * @param array $expected
  *
  * @dataProvider getFieldsWithVirtualRelationsAndEnumsDataProvider
  */
 public function testGetFieldsWithVirtualRelationsAndEnums(array $expected)
 {
     $className = 'Acme\\Entity\\Test';
     $config = [$className => ['config' => ['label' => 'Test Label', 'plural_label' => 'Test Plural Label', 'icon' => 'icon-test'], 'fields' => ['field1' => ['type' => 'integer', 'identifier' => true, 'config' => ['label' => 'Field 1']]], 'relations' => ['rel1' => ['target_class' => 'Acme\\EnumValue1', 'type' => 'ref-one', 'config' => ['label' => 'Enum Field']], 'rel2' => ['target_class' => 'Acme\\EnumValue2', 'type' => 'ref-many', 'config' => ['label' => 'Multi Enum Field']]]]];
     $this->prepare($config);
     $this->virtualFieldProvider->expects($this->once())->method('getVirtualFields')->with($className)->will($this->returnValue(['rel1', 'rel2']));
     $this->virtualRelationProvider->expects($this->once())->method('getVirtualRelations')->with($className)->will($this->returnValue(['virtual_relation' => ['relation_type' => 'oneToMany', 'related_entity_name' => 'OtherEntity', 'query' => ['select' => ['select expression'], 'join' => ['join expression']]]]));
     $this->virtualFieldProvider->expects($this->exactly(2))->method('getVirtualFieldQuery')->will($this->returnValueMap([[$className, 'rel1', ['select' => ['return_type' => 'enum', 'filter_by_id' => true]]], [$className, 'rel2', ['select' => ['return_type' => 'multiEnum', 'filter_by_id' => true]]]]));
     $result = $this->provider->getFields('Acme:Test', true, true);
     $this->assertEquals($expected, $result);
 }
 /**
  * Gets a field data type
  *
  * @param string $className
  * @param string $fieldName
  *
  * @return string
  */
 protected function getFieldType($className, $fieldName)
 {
     $result = null;
     if ($this->virtualFieldProvider->isVirtualField($className, $fieldName)) {
         // try to guess virtual column type
         $key = sprintf('%s::%s', $className, $fieldName);
         if (isset($this->virtualColumnOptions[$key]['return_type'])) {
             $result = $this->virtualColumnOptions[$key]['return_type'];
         }
     }
     return $result;
 }
 /**
  * @param QueryBuilder $qb
  * @param string $entityClass
  * @param string $fieldName
  * @return string
  */
 protected function getVirtualFieldExpression(QueryBuilder $qb, $entityClass, $fieldName)
 {
     $rootAlias = $this->getRootTableAlias($qb);
     $conditions = ['entity' => $rootAlias];
     $fieldConfig = $this->virtualFieldProvider->getVirtualFieldQuery($entityClass, $fieldName);
     $fieldConfigJoins = $fieldConfig['join'];
     $joins = $qb->getDQLPart('join');
     if (empty($joins)) {
         foreach ($fieldConfigJoins as $type => $typedFieldConfigJoins) {
             foreach ($typedFieldConfigJoins as $typedFieldConfigJoin) {
                 $join = new Join($type, $this->replaceAliases($conditions, $typedFieldConfigJoin['join']), $typedFieldConfigJoin['alias'], $typedFieldConfigJoin['conditionType'], $typedFieldConfigJoin['condition']);
                 $qb->add('join', [$rootAlias => $join], true);
                 $joins[$rootAlias][] = $join;
             }
         }
     }
     /** @var Join $join */
     foreach ($joins[$rootAlias] as $join) {
         $joinType = strtolower($join->getJoinType());
         if (array_key_exists($joinType, $fieldConfigJoins)) {
             foreach ($fieldConfigJoins[$joinType] as $fieldJoin) {
                 if (strtoupper($fieldJoin['conditionType']) == strtoupper($join->getConditionType())) {
                     $fixedJoin = $this->replaceAliases($conditions, $fieldJoin['join']);
                     if ($fixedJoin == $join->getJoin()) {
                         $conditions[$fieldJoin['alias']] = $join->getAlias();
                         $fixedCondition = $this->replaceAliases($conditions, $fieldJoin['condition']);
                         if ($fixedCondition != (string) $join->getCondition()) {
                             unset($conditions[$fieldJoin['alias']]);
                         }
                     }
                 }
             }
         }
     }
     return $this->replaceAliases($conditions, $fieldConfig['select']['expr']);
 }
 /**
  * Adds entity virtual fields to $result
  *
  * @param array         $result
  * @param ClassMetadata $metadata
  * @param bool          $applyExclusions
  * @param bool          $translate
  */
 protected function addVirtualFields(array &$result, ClassMetadata $metadata, $applyExclusions, $translate)
 {
     $className = $metadata->getName();
     $virtualFields = $this->virtualFieldProvider->getVirtualFields($className);
     foreach ($virtualFields as $fieldName) {
         if ($applyExclusions && $this->exclusionProvider->isIgnoredField($metadata, $fieldName)) {
             continue;
         }
         $query = $this->virtualFieldProvider->getVirtualFieldQuery($className, $fieldName);
         $fieldLabel = isset($query['select']['label']) ? $query['select']['label'] : $this->getFieldLabel($className, $fieldName);
         $this->addField($result, $fieldName, $query['select']['return_type'], $fieldLabel, false, $translate);
         if (isset($query['select']['filter_by_id']) && $query['select']['filter_by_id']) {
             $result[$fieldName]['related_entity_name'] = $metadata->getAssociationTargetClass($fieldName);
         }
     }
 }
Example #7
0
 protected function loadVirtualFields()
 {
     $entities = $this->cache->getEntities();
     foreach ($entities as $className => $entityData) {
         $virtualFields = $this->virtualFieldProvider->getVirtualFields($className);
         if (!empty($virtualFields)) {
             foreach ($virtualFields as $fieldName) {
                 if (null === $this->cache->getConfigurable($className, $fieldName)) {
                     $this->cache->saveConfigurable(false, $className, $fieldName);
                 }
             }
         }
         $virtualRelations = $this->virtualRelationProvider->getVirtualRelations($className);
         if (!empty($virtualRelations)) {
             foreach ($virtualRelations as $fieldName => $config) {
                 if (null === $this->cache->getConfigurable($className, $fieldName)) {
                     $this->cache->saveConfigurable(false, $className, $fieldName);
                 }
             }
         }
     }
 }