/**
  * Makes sure virtual fields for the given entity are loaded
  *
  * @param string $className
  */
 protected function ensureVirtualFieldsInitialized($className)
 {
     if (isset($this->virtualFields[$className])) {
         return;
     }
     $this->virtualFields[$className] = [];
     $metadata = $this->getManagerForClass($className)->getClassMetadata($className);
     $associationNames = $metadata->getAssociationNames();
     foreach ($associationNames as $associationName) {
         if (!$this->extendConfigProvider->hasConfig($className, $associationName)) {
             continue;
         }
         $extendFieldConfig = $this->extendConfigProvider->getConfig($className, $associationName);
         /** @var FieldConfigId $fieldConfigId */
         $fieldConfigId = $extendFieldConfig->getId();
         $fieldType = $fieldConfigId->getFieldType();
         if ($fieldType === 'enum') {
             $this->virtualFields[$className][$associationName] = ['query' => ['select' => ['expr' => sprintf('target.%s', $extendFieldConfig->get('target_field')), 'return_type' => $fieldType, 'filter_by_id' => true], 'join' => ['left' => [['join' => sprintf('entity.%s', $associationName), 'alias' => 'target']]]]];
         } elseif ($fieldType === 'multiEnum') {
             $this->virtualFields[$className][$associationName] = ['query' => ['select' => ['expr' => sprintf('entity.%s', ExtendHelper::getMultiEnumSnapshotFieldName($associationName)), 'return_type' => $fieldType, 'filter_by_id' => true]]];
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function postUpdate()
 {
     $extendConfigProvider = $this->configManager->getProvider('extend');
     $entityConfigs = $extendConfigProvider->getConfigs(null, true);
     foreach ($entityConfigs as $entityConfig) {
         if ($entityConfig->is('inherit', ExtendHelper::BASE_ENUM_VALUE_CLASS)) {
             $entityClassName = $entityConfig->getId()->getClassName();
             $schema = $entityConfig->get('schema', false, []);
             if (!empty($schema['doctrine'][$entityClassName]['repositoryClass'])) {
                 continue;
             }
             $schema['doctrine'][$entityClassName]['repositoryClass'] = 'Oro\\Bundle\\EntityExtendBundle\\Entity\\Repository\\EnumValueRepository';
             $schema['doctrine'][$entityClassName]['gedmo']['translation']['entity'] = 'Oro\\Bundle\\EntityExtendBundle\\Entity\\EnumValueTranslation';
             $entityConfig->set('schema', $schema);
             $this->configManager->persist($entityConfig);
         } elseif ($entityConfig->is('is_extend')) {
             $fieldConfigs = $extendConfigProvider->getConfigs($entityConfig->getId()->getClassName());
             foreach ($fieldConfigs as $fieldConfig) {
                 /** @var FieldConfigId $fieldConfigId */
                 $fieldConfigId = $fieldConfig->getId();
                 if ($fieldConfigId->getFieldType() !== 'multiEnum') {
                     continue;
                 }
                 $mappingClassName = $entityConfig->has('extend_class') ? $entityConfig->get('extend_class') : $entityConfig->getId()->getClassName();
                 $fieldName = $fieldConfigId->getFieldName();
                 $snapshotFieldName = ExtendHelper::getMultiEnumSnapshotFieldName($fieldName);
                 $schema = $entityConfig->get('schema', false, []);
                 if (!empty($schema['doctrine'][$mappingClassName]['fields'][$snapshotFieldName])) {
                     continue;
                 }
                 $schema['property'][$snapshotFieldName] = $snapshotFieldName;
                 $schema['doctrine'][$mappingClassName]['fields'][$snapshotFieldName] = ['column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName($fieldName), 'type' => 'string', 'nullable' => true, 'length' => ExtendHelper::MAX_ENUM_SNAPSHOT_LENGTH];
                 $entityConfig->set('schema', $schema);
                 $this->configManager->persist($entityConfig);
             }
         }
     }
 }
 public function testVisitDatasourceForMultiEnum()
 {
     $fieldType = 'multiEnum';
     $alias = 'c';
     $snapshotFieldName = ExtendHelper::getMultiEnumSnapshotFieldName(self::FIELD_NAME);
     $this->entityClassResolver->expects($this->any())->method('getEntityClass')->with(self::ENTITY_NAME)->will($this->returnValue(self::ENTITY_CLASS));
     $this->setExpectationForGetFields(self::ENTITY_CLASS, self::FIELD_NAME, $fieldType);
     $viewFieldConfig = new Config(new FieldConfigId('view', self::ENTITY_CLASS, self::FIELD_NAME, $fieldType));
     $this->viewConfigProvider->expects($this->any())->method('getConfig')->with(self::ENTITY_CLASS, self::FIELD_NAME)->will($this->returnValue($viewFieldConfig));
     $from = $this->getMockBuilder('Doctrine\\ORM\\Query\\Expr\\From')->disableOriginalConstructor()->getMock();
     $from->expects($this->once())->method('getAlias')->will($this->returnValue($alias));
     $from->expects($this->once())->method('getFrom')->will($this->returnValue(self::ENTITY_NAME));
     $qb = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $qb->expects($this->once())->method('getDQLPart')->with('from')->will($this->returnValue([$from]));
     $qb->expects($this->once())->method('addSelect')->with($alias . '.' . $snapshotFieldName)->will($this->returnSelf());
     $datasource = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\OrmDatasource')->disableOriginalConstructor()->getMock();
     $datasource->expects($this->once())->method('getQueryBuilder')->will($this->returnValue($qb));
     $config = $this->getDatagridConfiguration();
     $initialConfig = $config->toArray();
     $this->extension->visitDatasource($config, $datasource);
     $this->assertEquals(array_merge($initialConfig, ['columns' => [self::FIELD_NAME => ['data_name' => $snapshotFieldName]], 'sorters' => ['columns' => [self::FIELD_NAME => ['data_name' => $alias . '.' . $snapshotFieldName]]], 'filters' => ['columns' => [self::FIELD_NAME => ['data_name' => $alias . '.' . self::FIELD_NAME]]]]), $config->toArray());
 }
 public function testPostUpdateForMultiEnumFieldsInCustomEntity()
 {
     $entityConfig1 = new Config(new EntityConfigId('extend', 'Extend\\EnumValue1'));
     $entityConfig1->set('owner', ExtendScope::OWNER_CUSTOM);
     $entityConfig1->set('is_extend', true);
     $entityConfig1->set('schema', ['doctrine' => ['Extend\\EnumValue1' => ['fields' => [ExtendHelper::getMultiEnumSnapshotFieldName('field2') => ['column' => 'field2']]]]]);
     $entityConfig2 = new Config(new EntityConfigId('extend', 'Extend\\EnumValue2'));
     $fieldConfig1 = new Config(new FieldConfigId('extend', 'Extend\\EnumValue1', 'field1', 'multiEnum'));
     $fieldConfig2 = new Config(new FieldConfigId('extend', 'Extend\\EnumValue1', 'field2', 'multiEnum'));
     $fieldConfig3 = new Config(new FieldConfigId('extend', 'Extend\\EnumValue1', 'field3', 'enum'));
     $entityConfigs = [$entityConfig1, $entityConfig2];
     $fieldConfigs = [$fieldConfig1, $fieldConfig2, $fieldConfig3];
     $extendConfigProvider = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProvider')->disableOriginalConstructor()->getMock();
     $this->configManager->expects($this->once())->method('getProvider')->with('extend')->will($this->returnValue($extendConfigProvider));
     $extendConfigProvider->expects($this->at(0))->method('getConfigs')->with(null, true)->will($this->returnValue($entityConfigs));
     $extendConfigProvider->expects($this->at(1))->method('getConfigs')->with($entityConfig1->getId()->getClassName())->will($this->returnValue($fieldConfigs));
     $this->configManager->expects($this->once())->method('persist')->with($this->identicalTo($entityConfig1));
     $this->extension->postUpdate();
     $this->assertEquals(['doctrine' => ['Extend\\EnumValue1' => ['fields' => [ExtendHelper::getMultiEnumSnapshotFieldName('field1') => ['column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName('field1'), 'type' => 'string', 'nullable' => true, 'length' => ExtendHelper::MAX_ENUM_SNAPSHOT_LENGTH], ExtendHelper::getMultiEnumSnapshotFieldName('field2') => ['column' => 'field2']]]], 'property' => [ExtendHelper::getMultiEnumSnapshotFieldName('field1') => ExtendHelper::getMultiEnumSnapshotFieldName('field1')]], $entityConfig1->get('schema'));
 }
 /**
  * {@inheritdoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     $fields = $this->getFields($config);
     if (empty($fields)) {
         return;
     }
     $entityClassName = $this->entityClassResolver->getEntityClass($this->getEntityName($config));
     /** @var QueryBuilder $qb */
     $qb = $datasource->getQueryBuilder();
     $fromParts = $qb->getDQLPart('from');
     $alias = false;
     /** @var From $fromPart */
     foreach ($fromParts as $fromPart) {
         if ($this->entityClassResolver->getEntityClass($fromPart->getFrom()) == $entityClassName) {
             $alias = $fromPart->getAlias();
         }
     }
     if ($alias === false) {
         // add entity if it not exists in from clause
         $alias = 'o';
         $qb->from($entityClassName, $alias);
     }
     $relationIndex = 0;
     $relationTemplate = 'auto_rel_%d';
     foreach ($fields as $field) {
         $fieldName = $field->getFieldName();
         switch ($field->getFieldType()) {
             case 'enum':
                 $extendFieldConfig = $this->getFieldConfig('extend', $field);
                 $joinAlias = sprintf($relationTemplate, ++$relationIndex);
                 $qb->leftJoin(sprintf('%s.%s', $alias, $fieldName), $joinAlias);
                 $columnDataName = $fieldName;
                 $sorterDataName = sprintf('%s.%s', $joinAlias, $extendFieldConfig->get('target_field'));
                 $selectExpr = sprintf('%s as %s', $sorterDataName, $fieldName);
                 break;
             case 'multiEnum':
                 $columnDataName = ExtendHelper::getMultiEnumSnapshotFieldName($fieldName);
                 $sorterDataName = sprintf('%s.%s', $alias, $columnDataName);
                 $selectExpr = $sorterDataName;
                 break;
             default:
                 $columnDataName = $fieldName;
                 $sorterDataName = sprintf('%s.%s', $alias, $fieldName);
                 $selectExpr = $sorterDataName;
                 break;
         }
         $qb->addSelect($selectExpr);
         // set real "data name" for filters and sorters
         $config->offsetSetByPath(sprintf('[%s][%s][data_name]', FormatterConfiguration::COLUMNS_KEY, $fieldName), $columnDataName);
         $config->offsetSetByPath(sprintf('%s[%s][data_name]', SorterConfiguration::COLUMNS_PATH, $fieldName), $sorterDataName);
         $config->offsetSetByPath(sprintf('%s[%s][data_name]', FilterConfiguration::COLUMNS_PATH, $fieldName), sprintf('%s.%s', $alias, $fieldName));
     }
 }
Beispiel #6
0
 public function testGetMultiEnumSnapshotFieldName()
 {
     $this->assertEquals('testFieldSnapshot', ExtendHelper::getMultiEnumSnapshotFieldName('testField'));
 }
 public function testGetVirtualFieldQueryForMultiEnum()
 {
     $className = 'Test\\Entity';
     $this->initialize($className);
     $this->assertEquals(['select' => ['expr' => 'entity.' . ExtendHelper::getMultiEnumSnapshotFieldName('multiEnumField'), 'return_type' => 'multiEnum', 'filter_by_id' => true]], $this->provider->getVirtualFieldQuery($className, 'multiEnumField'));
 }
Beispiel #8
0
 /**
  * @param string $fieldName
  *
  * @return string
  */
 protected function getSnapshotFieldMethodSuffix($fieldName)
 {
     return Inflector::classify(ExtendHelper::getMultiEnumSnapshotFieldName($fieldName));
 }