getByEntityName example

throw new FileNotReadableException($filePath);
        }

        // read the first CSV line         $record = fgetcsv($fileHandle, 0, $delimiter$enclosure$escape);
        fclose($fileHandle);
        if (empty($record) || $record[0] === null) {
            throw new InvalidFileContentException($file->getFilename());
        }

        // construct the mapping from the given CSV line data         $definition = $this->definitionInstanceRegistry->getByEntityName($sourceEntity);
        $keyLookupTable = $this->getKeyLookupTable($context$sourceEntity);

        $mappings = new MappingCollection();
        foreach ($record as $index => $column) {
            $mappings->add(new Mapping(
                $this->guessKeyFromMappedKey($keyLookupTable$column$definition),
                $column,
                $index
            ));
        }

        
#[Route(path: '/api/_action/clone/{entity}/{id}', name: 'api.clone', methods: ['POST'], requirements: ['version' => '\d+', 'entity' => '[a-zA-Z-]+', 'id' => '[0-9a-f]{32}'])]     public function clone(Context $context, string $entity, string $id, Request $request): JsonResponse
    {
        $behavior = new CloneBehavior(
            $request->request->all('overwrites'),
            $request->request->getBoolean('cloneChildren', true)
        );

        $entity = $this->urlToSnakeCase($entity);

        $definition = $this->definitionRegistry->getByEntityName($entity);
        $missing = $this->validateAclPermissions($context$definition, AclRoleDefinition::PRIVILEGE_CREATE);
        if ($missing) {
            throw ApiException::missingPrivileges([$missing]);
        }

        /** @var EntityWrittenContainerEvent $eventContainer */
        $eventContainer = $context->scope(Context::CRUD_API_SCOPE, function DContext $context) use ($definition$id$behavior): EntityWrittenContainerEvent {
            $entityRepo = $this->definitionRegistry->getRepository($definition->getEntityName());

            return $entityRepo->clone($id$context, null, $behavior);
        });

        

    }

    public function getDefinition(): EntityDefinition
    {
        $instanceRegistry = new StaticDefinitionInstanceRegistry(
            [ProductDefinition::class, ProductManufacturerDefinition::class, ProductTranslationDefinition::class],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        return $instanceRegistry->getByEntityName('product');
    }
}

/** * @internal */
class CustomFilter extends Filter
{
    public function getFields(): array
    {
        return [];
    }
$request->request->set('order', self::DEFAULT_SEARCH_SORT);
        }

        $criteria->addState(self::STATE);
        if (!Feature::isActive('v6.6.0.0')) {
            $context->getContext()->addState(ProductListingFeaturesSubscriber::HANDLED_STATE);
        }

        $criteria = $this->criteriaBuilder->handleRequest(
            $request,
            $criteria,
            $this->registry->getByEntityName('product'),
            $context->getContext()
        );

        $this->processor->prepare($request$criteria$context);

        $this->eventDispatcher->dispatch(
            new ProductSearchCriteriaEvent($request$criteria$context),
            ProductEvents::PRODUCT_SEARCH_CRITERIA
        );

        $response = $this->getDecorated()->load($request$context$criteria);

        
class ApiCriteriaValidator
{
    /** * @internal */
    public function __construct(private readonly DefinitionInstanceRegistry $registry)
    {
    }

    public function validate(string $entity, Criteria $criteria, Context $context): void
    {
        $definition = $this->registry->getByEntityName($entity);

        foreach ($criteria->getAllFields() as $accessor) {
            $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

            foreach ($fields as $field) {
                if (!$field instanceof Field) {
                    continue;
                }

                if ($field->getFlag(ApiCriteriaAware::class)) {
                    continue;
                }
$event->markAsUsed(
                array_merge(
                    ...array_map(fn (string $ids) => json_decode($ids, true, \JSON_THROW_ON_ERROR)$usedMediaIds)
                )
            );
        }
    }

    private function getTableName(string $entity): string
    {
        $definition = $this->definitionRegistry->getByEntityName($entity);

        $customFields = $definition->getField('customFields');

        $table = $definition->getEntityName();

        if ($customFields instanceof TranslatedField) {
            $table = $definition->getTranslationDefinition()?->getEntityName() ?? $table;
        }

        return $table;
    }

    
public function batchUpdate(array $updateIds, string $entity, Context $context): void
    {
        $updateIds = Uuid::fromHexToBytesList(array_unique($updateIds));
        if (empty($updateIds)) {
            return;
        }

        $bag = new TreeUpdaterBag();

        $this->updateEntityStatement = null;

        $definition = $this->registry->getByEntityName($entity);

        // the batch update does not support versioning, so fallback to single updates         if ($definition->isVersionAware() && $context->getVersionId() !== Defaults::LIVE_VERSION) {
            foreach ($updateIds as $id) {
                $this->singleUpdate(Uuid::fromBytesToHex($id)$entity$context);
            }

            return;
        }

        // 1. fetch parents until all ids have reached parent_id === null


    public function prefetchExistences(WriteParameterBag $parameters): void
    {
        $primaryKeyBag = $this->primaryKeyBag = $parameters->getPrimaryKeyBag();

        if ($primaryKeyBag->isPrefetchingCompleted()) {
            return;
        }

        foreach ($primaryKeyBag->getPrimaryKeys() as $entity => $pks) {
            $this->prefetch($this->definitionInstanceRegistry->getByEntityName($entity)$pks$parameters);
        }

        $primaryKeyBag->setPrefetchingCompleted(true);
    }

    /** * {@inheritdoc} */
    public function getExistence(EntityDefinition $definition, array $primaryKey, array $data, WriteCommandQueue $commandQueue): EntityExistence
    {
        $state = $this->getCurrentState($definition$primaryKey$commandQueue);

        
/** * @param array<string, array<string, mixed>> $data */
    private function extractJsonUpdate(array $data, EntityExistence $existence, WriteParameterBag $parameters): void
    {
        foreach ($data as $storageName => $attributes) {
            $entityName = $existence->getEntityName();
            if (!$entityName) {
                continue;
            }

            $definition = $this->definitionRegistry->getByEntityName($entityName);

            $pks = array_combine(
                array_keys($existence->getPrimaryKey()),
                array_map(
                    function Dstring $pkFieldStorageName) use ($definition$existence$parameters): mixed {
                        $pkFieldValue = $existence->getPrimaryKey()[$pkFieldStorageName];
                        /** @var Field|null $field */
                        $field = $definition->getFields()->getByStorageName($pkFieldStorageName);
                        if (!$field) {
                            return $pkFieldValue;
                        }

                        
$writeContextMockWithVersionId->expects(static::exactly(2))->method('getContext')->willReturn(Context::createDefaultContext());

        $registry = new StaticDefinitionInstanceRegistry(
            [
                VersionManagerTestDefinition::class,
            ],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        $entityWriteResult = $this->versionManager->clone(
            $registry->getByEntityName('product'),
            Uuid::randomHex(),
            Uuid::randomHex(),
            Uuid::randomHex(),
            $writeContextMock,
            $this->createMock(CloneBehavior::class)
        );

        static::assertNotEmpty($entityWriteResult);
        static::assertSame('insert', $entityWriteResult['product'][0]->getOperation());
        static::assertSame('product', $entityWriteResult['product'][0]->getEntityName());
    }

    


    private function hydrateEntityAggregation(EntityAggregation $aggregation, array $result, Context $context): EntityResult
    {
        if (\array_key_exists($aggregation->getName()$result)) {
            $result = $result[$aggregation->getName()];
        }

        $ids = array_column($result['buckets'], 'key');

        if (empty($ids)) {
            $definition = $this->registry->getByEntityName($aggregation->getEntity());
            /** @var EntityCollection<Entity> $class */
            $class = $definition->getCollectionClass();

            return new EntityResult($aggregation->getName()new $class());
        }
        $repository = $this->registry->getRepository($aggregation->getEntity());

        $entities = $repository->search(new Criteria($ids)$context);

        return new EntityResult($aggregation->getName()$entities->getEntities());
    }

    

            ''
        );

        $commands = [$insert];

        foreach ($writtenEvents as $items) {
            if (\count($items) === 0) {
                continue;
            }

            $definition = $this->registry->getByEntityName($items[0]->getEntityName());
            $entityName = $definition->getEntityName();

            if (!$definition->isVersionAware()) {
                continue;
            }

            if (mb_strpos('version', $entityName) === 0) {
                continue;
            }

            /** @var EntityWriteResult $item */
            

        $criteriaObject = $this->prepareCriteria($entityName$criteria);

        return $this->registry->getRepository($entityName)->aggregate($criteriaObject$this->context);
    }

    /** * @param array<string, mixed> $criteria */
    private function prepareCriteria(string $entityName, array $criteria): Criteria
    {
        $definition = $this->registry->getByEntityName($entityName);
        $criteriaObject = new Criteria();

        $this->criteriaBuilder->fromArray($criteria$criteriaObject$definition$this->context);

        $missingPermissions = $this->criteriaValidator->validate($entityName$criteriaObject$this->context);

        if (!empty($missingPermissions)) {
            throw new MissingPrivilegeException($missingPermissions);
        }

        return $criteriaObject;
    }
public function __construct(
        private readonly DefinitionInstanceRegistry $registry,
        private readonly Connection $connection
    ) {
    }

    /** * @param array<string> $parentIds */
    public function update(string $entity, array $parentIds, Context $context): void
    {
        $definition = $this->registry->getByEntityName($entity);

        if (empty($parentIds)) {
            return;
        }

        RetryableTransaction::retryable($this->connection, function D) use ($definition$parentIds$context): void {
            $this->trySingleUpdate($definition$parentIds$context);
        });
    }

    /** * @param array<string> $parentIds */

    public function validate(string $entity, Criteria $criteria, Context $context): array
    {
        $privilege = $entity . ':' . AclRoleDefinition::PRIVILEGE_READ;

        $missing = [];

        if (!$context->isAllowed($privilege)) {
            $missing[] = $privilege;
        }

        $definition = $this->registry->getByEntityName($entity);

        foreach ($criteria->getAssociations() as $field => $nested) {
            $association = $definition->getField($field);

            if (!$association instanceof AssociationField) {
                throw new AssociationNotFoundException($field);
            }

            $reference = $association->getReferenceDefinition()->getEntityName();
            if ($association instanceof ManyToManyAssociationField) {
                $reference = $association->getToManyReferenceDefinition()->getEntityName();
            }
Home | Imprint | This part of the site doesn't use cookies.