Skip to content

[DoctrineBridge] Validator throwing a doctrine mapping exception when using an indexBy on a sub property #46804

Closed
@da-anda

Description

@da-anda

Symfony version(s) affected

5.4.10

Description

This issue is related to #39667 and #37982

If indexBy is referring to a subproperty of the relation, a Doctrine\ORM\Mapping\MappingException is thrown when the validator tries to resolve the metadata. Symfony/Doctrine itself are working fine doing this and correctly indexing the relations, but as soon as the validator tries to resolve the metadata, the below exception is thrown. I came across this issue after I installed the api_platform bundle which instantly threw the exception in my face.

Exception:

Cannot find a field on 'App\Entity\ArticleAttribute' that is mapped to column 'attribute.name'. Either the field does not exist or an association exists but it has multiple join columns.

How to reproduce

Example class structure:

use ApiPlatform\Core\Annotation\ApiResource;

/**
 * @ApiResource()
 */
class Article {
    /**
     * @var ArticleAttribute[]
     * @ORM\OneToMany(targetEntity=ArticleAttribute::class, mappedBy="article", cascade={"all"}, indexBy="attribute.name")
     */
    private $attributes;
}

class ArticleAttribute {
    /**
     * @var Attribute
     * @ORM\ManyToOne(targetEntity=Attribute::class)
     * @ORM\JoinColumn(nullable=false)
     */
    private $attribute;
}

class Attribute {
    /**
     * @var string
     * @ORM\Column(type="string", length=30, unique=true)
     */
    private $name;
}

In my case the exception has been triggered by api-platform, but I suppose any attempt to validate the article entity would cause this exception

Possible Solution

No response

Additional Context

Trace:

Doctrine\ORM\Mapping\MappingException:
Cannot find a field on 'App\Entity\ArticleAttribute' that is mapped to column 'attribute.name'. Either the field does not exist or an association exists but it has multiple join columns.

  at vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php:681
  at Doctrine\ORM\Mapping\MappingException::noFieldNameFoundForColumn('App\\Entity\\ArticleAttribute', 'attribute.name')
     (vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataInfo.php:3404)
  at Doctrine\ORM\Mapping\ClassMetadataInfo->getFieldForColumn('attribute.name')
     (vendor\symfony\doctrine-bridge\PropertyInfo\DoctrineExtractor.php:99)
  at Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor->getTypes('App\\Entity\\ArticleAttribute', 'attributes', array())
     (vendor\symfony\property-info\PropertyInfoExtractor.php:112)
  at Symfony\Component\PropertyInfo\PropertyInfoExtractor->extract(object(RewindableGenerator), 'getTypes', array('App\\Entity\\Article', 'attributes', array()))
     (vendor\symfony\property-info\PropertyInfoExtractor.php:74)
  at Symfony\Component\PropertyInfo\PropertyInfoExtractor->getTypes('App\\Entity\\Article', 'attributes')
     (vendor\symfony\validator\Mapping\Loader\PropertyInfoLoader.php:68)
  at Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader->loadClassMetadata(object(ClassMetadata))
     (vendor\symfony\validator\Mapping\Loader\LoaderChain.php:54)
  at Symfony\Component\Validator\Mapping\Loader\LoaderChain->loadClassMetadata(object(ClassMetadata))
     (vendor\symfony\validator\Mapping\Factory\LazyLoadingMetadataFactory.php:101)
  at Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory->getMetadataFor('App\\Entity\\Article')
     (vendor\symfony\validator\Validator\RecursiveValidator.php:76)
  at Symfony\Component\Validator\Validator\RecursiveValidator->getMetadataFor('App\\Entity\\Article')
     (vendor\symfony\validator\Validator\TraceableValidator.php:50)
  at Symfony\Component\Validator\Validator\TraceableValidator->getMetadataFor('App\\Entity\\Article')
     (vendor\api-platform\core\src\Bridge\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory.php:103)
  at ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory->create('App\\Entity\\Article', 'articleNumber', array('enable_getter_setter_extraction' => true))
     (vendor\api-platform\core\src\Metadata\Property\Factory\CachedPropertyMetadataFactory.php:47)
  at ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory->ApiPlatform\Core\Metadata\Property\Factory\{closure}()
     (vendor\api-platform\core\src\Cache\CachedTrait.php:44)
  at ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory->getCached('property_metadata_d5d1043750e465746f2c166191c0dd40', object(Closure))
     (vendor\api-platform\core\src\Metadata\Property\Factory\CachedPropertyMetadataFactory.php:48)
  at ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory->create('App\\Entity\\Article', 'articleNumber', array('enable_getter_setter_extraction' => true))
     (vendor\api-platform\core\src\JsonSchema\SchemaFactory.php:145)
  at ApiPlatform\Core\JsonSchema\SchemaFactory->buildSchema('App\\Entity\\Article', 'jsonld', 'output', null, null, object(Schema), array(), false)
     (vendor\api-platform\core\src\Hydra\JsonSchema\SchemaFactory.php:77)
  at ApiPlatform\Core\Hydra\JsonSchema\SchemaFactory->buildSchema('App\\Entity\\Article', 'jsonld', 'output', null, null, object(Schema), array(), false)
     (vendor\api-platform\core\src\JsonSchema\TypeFactory.php:151)
  at ApiPlatform\Core\JsonSchema\TypeFactory->getClassType('App\\Entity\\Article', 'jsonld', null, array(), object(Schema))
     (vendor\api-platform\core\src\JsonSchema\TypeFactory.php:84)
  at ApiPlatform\Core\JsonSchema\TypeFactory->makeBasicType(object(Type), 'jsonld', null, array(), object(Schema))
     (vendor\api-platform\core\src\JsonSchema\TypeFactory.php:71)
  at ApiPlatform\Core\JsonSchema\TypeFactory->getType(object(Type), 'jsonld', null, array(), object(Schema))
     (vendor\api-platform\core\src\JsonSchema\TypeFactory.php:67)
  at ApiPlatform\Core\JsonSchema\TypeFactory->getType(object(Type), 'jsonld', null, array(), object(Schema))
     (vendor\api-platform\core\src\JsonSchema\SchemaFactory.php:233)
  at ApiPlatform\Core\JsonSchema\SchemaFactory->buildPropertySchema(object(Schema), 'Product.jsonld', 'articles', object(PropertyMetadata), array(), 'jsonld')
     (vendor\api-platform\core\src\JsonSchema\SchemaFactory.php:155)
  at ApiPlatform\Core\JsonSchema\SchemaFactory->buildSchema('App\\Entity\\Product', 'jsonld', 'output', 'item', 'get', object(Schema), array(), false)
     (vendor\api-platform\core\src\Hydra\JsonSchema\SchemaFactory.php:77)
  at ApiPlatform\Core\Hydra\JsonSchema\SchemaFactory->buildSchema('App\\Entity\\Product', 'jsonld', 'output', 'item', 'get', object(Schema), null, false)
     (vendor\api-platform\core\src\OpenApi\Factory\OpenApiFactory.php:163)
  at ApiPlatform\Core\OpenApi\Factory\OpenApiFactory->collectPaths(object(ResourceMetadata), 'App\\Entity\\Product', 'item', array('base_url' => '/'), object(Paths), array(), object(ArrayObject))
     (vendor\api-platform\core\src\OpenApi\Factory\OpenApiFactory.php:96)
  at ApiPlatform\Core\OpenApi\Factory\OpenApiFactory->__invoke(array('base_url' => '/'))
     (vendor\api-platform\core\src\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction.php:64)
  at ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction->__invoke(object(Request))
     (vendor\api-platform\core\src\Bridge\Symfony\Bundle\Action\SwaggerUiAction.php:128)
  at ApiPlatform\Core\Bridge\Symfony\Bundle\Action\SwaggerUiAction->__invoke(object(Request))
     (vendor\symfony\http-kernel\HttpKernel.php:152)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor\symfony\http-kernel\HttpKernel.php:74)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor\symfony\http-kernel\Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (vendor\symfony\runtime\Runner\Symfony\HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (vendor\autoload_runtime.php:35)
  at require_once('D:\\xampp\\htdocs\\eb-angebotsverwaltung\\vendor\\autoload_runtime.php')
     (D:\xampp\htdocs\eb-angebotsverwaltung\public\index.php:5)                

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions