Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
da-anda opened this issue Jun 29, 2022 · 7 comments

Comments

@da-anda
Copy link

da-anda commented Jun 29, 2022

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)                
@da-anda da-anda added the Bug label Jun 29, 2022
@OskarStark OskarStark changed the title [DoctrineBridge] Validator throwing a doctrine mapping exception when using an "indexBy" on a sub property [DoctrineBridge] Validator throwing a doctrine mapping exception when using an indexBy on a sub property Jul 14, 2022
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@da-anda
Copy link
Author

da-anda commented Mar 2, 2023

yes, I had to work around it by changing the index to use to attribute_id along with other wrapping code, but it is still a valid report unless using a relation property as index is not allowed in general (albeit it works, except for the validation)

@carsonbot carsonbot removed the Stalled label Mar 2, 2023
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@da-anda
Copy link
Author

da-anda commented Sep 4, 2023

haven't tried it, but if nobody fixed it, it should still be relevant

@carsonbot carsonbot removed the Stalled label Sep 4, 2023
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants