addFields example

$repo = new EntityRepository(
            $this->createMock(EntityDefinition::class),
            $reader,
            $this->createMock(VersionManager::class),
            $this->createMock(EntitySearcherInterface::class),
            $this->createMock(EntityAggregatorInterface::class),
            $eventDispatcher,
            new EntityLoadedEventFactory($this->createMock(DefinitionInstanceRegistry::class))
        );

        $criteria = new Criteria();
        $criteria->addFields(['foo']);
        $repo->search($criteria, Context::createDefaultContext());

        static::assertTrue(true);
        static::assertInstanceOf(EntityLoadedContainerEvent::class$event);

        $events = $event->getEvents();
        static::assertInstanceOf(NestedEventCollection::class$events);
        static::assertCount(1, $events);

        $partialEvent = $events->first();

        
$nested = $criteria->getAssociation($propertyName);

                $this->parse($association$nested$ref$context, null);

                if ($field instanceof TranslationsAssociationField) {
                    $nested->setLimit(null);
                }
            }
        }

        if (isset($payload['fields'])) {
            $criteria->addFields($payload['fields']);
        }

        $searchException->tryToThrow();

        $this->validator->validate($definition->getEntityName()$criteria$context);

        return $criteria;
    }

    /** * @param list<array{order: string, type: string, field: string}> $sorting * * @return list<FieldSorting> */
$product = (new ProductBuilder($ids, 'p1'))
            ->price(100)
            ->categories(['c1', 'c2'])
            ->visibility()
            ->manufacturer('m1');

        $this->getContainer()->get('product.repository')
            ->create([$product->build()], Context::createDefaultContext());

        $criteria = new Criteria();
        $criteria->addFields(['productNumber', 'name', 'categories.name']);

        $values = $this->getContainer()
            ->get('product.repository')
            ->search($criteria, Context::createDefaultContext());

        $entity = $values->first();

        static::assertInstanceOf(PartialEntity::class$entity);
        static::assertSame('p1', $entity->get('productNumber'));
        static::assertSame('p1', $entity->get('name'));
        static::assertNull($entity->get('active'));

        
new ProductBuilder($ids, 'p.1'))
                    ->price(130)
                    ->prices('rule-a', 150)
                    ->visibility()
                    ->build(),
            ], Context::createDefaultContext());

        $salesChannelContext = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $criteria = new Criteria([$ids->get('p.1')]);
        $criteria->addFields(['id', 'cheapestPrice', 'taxId', 'price']);

        /** @var SalesChannelProductEntity $productEntity */
        $productEntity = $this->getContainer()
            ->get('sales_channel.product.repository')
            ->search($criteria$salesChannelContext)
            ->first();

        static::assertInstanceOf(CheapestPrice::class$productEntity->get('cheapestPrice'));
        static::assertInstanceOf(CalculatedCheapestPrice::class$productEntity->get('calculatedCheapestPrice'));
    }

    
$result = $resolver->resolve($criteria$this->registry->get(TestDefinition::class));

        static::assertEquals($expected$result);
    }

    public static function resolveFieldsProvider(): \Generator
    {
        yield 'empty criteria' => [new Criteria()[]];

        yield 'criteria with association field' => [
            (new Criteria())
                ->addFields(['name', 'relation.name']),
            ['name' => [], 'relation' => ['name' => []]],
        ];

        yield 'criteria with runtime field' => [
            (new Criteria())
                ->addFields(['name', 'variation']),

            ['name' => [], 'variation' => [], 'relation' => ['name' => []]],
        ];
    }
}

#[Route(path: '/store-api/product-listing/{categoryId}', name: 'store-api.product.listing', methods: ['POST'], defaults: ['_entity' => 'product'])]     public function load(string $categoryId, Request $request, SalesChannelContext $context, Criteria $criteria): ProductListingRouteResponse
    {
        $criteria->addFilter(
            new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_ALL)
        );
        $criteria->setTitle('product-listing-route::loading');

        $categoryCriteria = new Criteria([$categoryId]);
        $categoryCriteria->setTitle('product-listing-route::category-loading');
        $categoryCriteria->addFields(['productAssignmentType', 'productStreamId']);
        $categoryCriteria->setLimit(1);

        /** @var PartialEntity|null $category */
        $category = $this->categoryRepository->search($categoryCriteria$context->getContext())->first();
        if (!$category) {
            throw ProductException::categoryNotFound($categoryId);
        }

        $this->extendCriteria($context$criteria$category);

        $entities = $this->listingLoader->load($criteria$context);

        
Home | Imprint | This part of the site doesn't use cookies.