/**
  * @param EntityMetadata $entityMetadata
  * @param ClassMetadata $classMetadata
  * @param string $className
  */
 protected function addDoctrineInverseAssociations(EntityMetadata $entityMetadata, ClassMetadata $classMetadata, $className)
 {
     $allMetadata = $this->doctrineHelper->getAllMetadata();
     foreach ($allMetadata as $metadata) {
         if ($metadata == $classMetadata) {
             // Skip own class metadata
             continue;
         }
         $currentClassName = $metadata->getName();
         $associationMappings = $metadata->getAssociationsByTargetClass($className);
         foreach ($associationMappings as $fieldName => $associationMapping) {
             if (isset($associationMapping['type']) && $associationMapping['type'] === ClassMetadataInfo::MANY_TO_MANY || isset($associationMapping['mappedBy'])) {
                 // Skip "mapped by" and many-to-many as it's included on other side.
                 continue;
             }
             $associationMapping['mappedBySourceEntity'] = false;
             $mergeModes = [MergeModes::UNITE];
             if ($associationMapping['type'] === ClassMetadataInfo::ONE_TO_ONE) {
                 // for fields with ONE_TO_ONE relation Unite strategy is impossible, so Replace is used
                 $mergeModes = [MergeModes::REPLACE];
             }
             $fieldMetadata = $this->metadataFactory->createFieldMetadata(array('field_name' => $this->createInverseAssociationFieldName($currentClassName, $fieldName), 'merge_modes' => $mergeModes, 'source_field_name' => $fieldName, 'source_class_name' => $currentClassName), $associationMapping);
             $entityMetadata->addFieldMetadata($fieldMetadata);
         }
     }
 }
 /**
  * Merge fields
  *
  * @param EntityData $data
  */
 public function run(EntityData $data)
 {
     $masterEntity = $data->getMasterEntity();
     foreach ($data->getEntities() as $entity) {
         if (!$this->doctrineHelper->isEntityEqual($masterEntity, $entity)) {
             $this->entityManager->remove($entity);
         }
     }
 }
 /**
  * Get entities that are not master.
  *
  * @param EntityData $entityData
  * @return array
  */
 protected function getEntitiesToClear(EntityData $entityData)
 {
     $result = array();
     $masterEntity = $entityData->getMasterEntity();
     foreach ($entityData->getEntities() as $entity) {
         if (!$this->doctrineHelper->isEntityEqual($masterEntity, $entity)) {
             $result[] = $entity;
         }
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function handle(MassActionHandlerArgs $args)
 {
     $massAction = $args->getMassAction();
     $options = $massAction->getOptions()->toArray();
     if (empty($options['entity_name'])) {
         throw new InvalidArgumentException('Entity name is missing.');
     }
     $entityIdentifier = $this->doctrineHelper->getSingleIdentifierFieldName($options['entity_name']);
     $entityIds = $this->getIdsFromResult($args->getResults(), $entityIdentifier);
     $entities = $this->doctrineHelper->getEntitiesByIds($options['entity_name'], $entityIds);
     return new MassActionResponse(true, null, array('entities' => $entities, 'entity_name' => $options['entity_name'], 'options' => $options));
 }
 /**
  * {@inheritdoc}
  */
 public function setValue($entity, FieldMetadata $metadata, $value)
 {
     $oldRelatedEntities = array();
     foreach ($this->getValue($entity, $metadata) as $oldRelatedEntity) {
         $oldRelatedEntities[$this->doctrineHelper->getEntityIdentifierValue($oldRelatedEntity)] = $oldRelatedEntity;
     }
     foreach ($value as $relatedEntity) {
         $this->setRelatedEntityValue($relatedEntity, $metadata, $entity);
         unset($oldRelatedEntities[$this->doctrineHelper->getEntityIdentifierValue($relatedEntity)]);
     }
     foreach ($oldRelatedEntities as $oldRelatedEntity) {
         $this->setRelatedEntityValue($oldRelatedEntity, $metadata, null);
     }
 }
Exemple #6
0
 /**
  * {@inheritdoc}
  */
 public function merge(FieldData $fieldData)
 {
     $entityData = $fieldData->getEntityData();
     $masterEntity = $entityData->getMasterEntity();
     $fieldMetadata = $fieldData->getMetadata();
     $entities = $entityData->getEntities();
     $relatedEntities = [];
     foreach ($entities as $entity) {
         $values = $this->accessor->getValue($entity, $fieldMetadata);
         foreach ($values as $value) {
             $key = $this->doctrineHelper->getEntityIdentifierValue($value);
             $relatedEntities[$key] = $value;
         }
     }
     $collection = new ArrayCollection(array_values($relatedEntities));
     $this->accessor->setValue($masterEntity, $fieldMetadata, $collection);
 }
 public function testGetAllMetadata()
 {
     $className = 'TestEntity';
     $expectedResult = array($this->metadata);
     $this->entityManager->expects($this->once())->method('getMetadataFactory')->will($this->returnValue($this->metadataFactory));
     $this->metadataFactory->expects($this->once())->method('getAllMetadata')->will($this->returnValue($expectedResult));
     $this->assertEquals($expectedResult, $this->doctrineHelper->getAllMetadata($className));
 }
 /**
  * @param string $entityName
  * @param array  $entityIds
  * @return EntityData
  */
 public function createEntityDataByIds($entityName, array $entityIds)
 {
     $entities = $this->doctrineHelper->getEntitiesByIds($entityName, $entityIds);
     return $this->createEntityData($entityName, $entities);
 }