ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension::applyToItem PHP Метод

applyToItem() публичный метод

{@inheritdoc} The context may contain serialization groups which helps defining joined entities that are readable.
public applyToItem ( Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = [] )
$queryBuilder Doctrine\ORM\QueryBuilder
$queryNameGenerator ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface
$resourceClass string
$identifiers array
$operationName string
$context array
    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = [])
    {
        $options = [];
        if (null !== $operationName) {
            $options = ['item_operation_name' => $operationName];
        }
        $forceEager = $this->isForceEager($resourceClass, $options);
        if (isset($context['groups'])) {
            $groups = ['serializer_groups' => $context['groups']];
        } elseif (isset($context['resource_class'])) {
            $groups = $this->getSerializerGroups($context['resource_class'], $options, isset($context['api_denormalize']) ? 'denormalization_context' : 'normalization_context');
        } else {
            $groups = $this->getSerializerGroups($resourceClass, $options, 'normalization_context');
        }
        $this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $queryBuilder->getRootAliases()[0], $groups);
    }

Usage Example

 public function testResourceClassNotFoundExceptionPropertyNameCollection()
 {
     $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
     $resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata());
     $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
     $propertyNameCollectionFactoryProphecy->create(UnknownDummy::class)->willThrow(new ResourceClassNotFoundException());
     $relationPropertyMetadata = new PropertyMetadata();
     $relationPropertyMetadata = $relationPropertyMetadata->withReadableLink(true);
     $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
     $propertyMetadataFactoryProphecy->create(Dummy::class, 'relation', [])->willReturn($relationPropertyMetadata);
     $classMetadataProphecy = $this->prophesize(ClassMetadata::class);
     $classMetadataProphecy->associationMappings = ['relation' => ['fetch' => 2, 'targetEntity' => UnknownDummy::class, 'joinColumns' => [['nullable' => false]]]];
     $emProphecy = $this->prophesize(EntityManager::class);
     $emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
     $emProphecy->getClassMetadata(UnknownDummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
     $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
     $queryBuilderProphecy->getRootAliases()->willReturn(['o']);
     $queryBuilderProphecy->getEntityManager()->willReturn($emProphecy);
     $queryBuilderProphecy->innerJoin('o.relation', 'relation_a1')->shouldBeCalled(1);
     $orderExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30, true);
     $orderExtensionTest->applyToItem($queryBuilderProphecy->reveal(), new QueryNameGenerator(), Dummy::class, []);
 }