Skip to content
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

How to access nested reference property? #1019

Closed
StanislavUngr opened this issue Feb 27, 2020 · 5 comments
Closed

How to access nested reference property? #1019

StanislavUngr opened this issue Feb 27, 2020 · 5 comments

Comments

@StanislavUngr
Copy link

Hello guys. So I have this issue where i need to access 2nd level reference's property. How do I do this?

This is my schema:

Entity\City:
   postalCodes: string[];
Entity\Address:
   city: Entity\City
   postalCode: random of (Entity\City->postalCodes)

YAML for alice:

App\Entity\City\City:
  city_{1..100}:
    __construct:
      name: <city()>
      district: '@district_*'
    postalCodes (unique): 5x <postcode()>
App\Entity\Address\Address:
  address_{1..100}:
    __construct:
      city: '@city_*'
      postalCode: '<randomElement(@self->postalCodes)>'
      type: <randomEnum(AddressType)>

However i get errors everytime i try to invoke <randomElement(@self->postalCodes)>:

[Nelmio\Alice\Throwable\Exception\ObjectNotFoundException]
  Could not find the object "address_1" of the class "App\Entity\Address\Address".

or <randomElement($city->postalCodes)>:

[Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueException]
  Could not evaluate the expression "$city".

It seems only 1st level of nesting is allowed.

Do you have any fixes/workarounds how to solve this? Thanks in advance..

@lugus
Copy link

lugus commented Feb 28, 2020

Hello,

Like you have 100 city and 100 address, maybe you could try this:

App\Entity\Address\Address:
  address_{1..100}:
    __construct:
      city: '@city_<current()>'
      postalCode: '<randomElement(@city_<current()>->postalCodes)>'
      type: <randomEnum(AddressType)>

@StanislavUngr
Copy link
Author

StanislavUngr commented Mar 2, 2020

Hello,

although this is a temporary fix and destroys logic of 1:N relation, for our testing purposes its enough atm.

Thanks for your help 👍

PS: PR to implement this feature is welcomed, of course 😄

@theofidry
Copy link
Member

The first variant cannot indeed work due to conflicting with the existing features.

The second however should work, but maybe @self->city->postalCodes will do?

@J3m5
Copy link

J3m5 commented Jun 28, 2021

The @self->city->postalCodes syntax doesn't works unfortunately, is there another way to access properties through multiple relations ?

php.CRITICAL: Uncaught Error: An error occurred while denormalizing the fixture "social_request_{1..150}" (App\Entity\SocialRequest): Could not parse the token "@self->employee->socialWorks" (type: PROPERTY_REFERENCE_TYPE). {"exception":"[object] (Error(code: 0): An error occurred while denormalizing the fixture \"social_request_{1..150}\" (App\\Entity\\SocialRequest): Could not parse the token \"@self->employee->socialWorks\" (type: PROPERTY_REFERENCE_TYPE). at /home/www-data/api/vendor/nelmio/alice/src/FixtureBuilder/Denormalizer/Fixture/TolerantFixtureDenormalizer.php:62)\n[previous exception] [object] (Nelmio\\Alice\\Throwable\\Exception\\FixtureBuilder\\ExpressionLanguage\\ParseException(code: 0): Could not parse the token \"@self->employee->socialWorks\" (type: PROPERTY_REFERENCE_TYPE). at /home/www-data/api/vendor/nelmio/alice/src/Throwable/Exception/FixtureBuilder/ExpressionLanguage/ExpressionLanguageExceptionFactory.php:47)"} []
php_1       | 
php_1       | In TolerantFixtureDenormalizer.php line 62:
php_1       |                                                                                
php_1       |   An error occurred while denormalizing the fixture "social_request_{1..150}"  
php_1       |    (App\Entity\SocialRequest): Could not parse the token "@self->employee->so  
php_1       |   cialWorks" (type: PROPERTY_REFERENCE_TYPE).                                  
php_1       |                                                                                
php_1       | 
php_1       | In ExpressionLanguageExceptionFactory.php line 47:
php_1       |                                                                                
php_1       |   Could not parse the token "@self->employee->socialWorks" (type: PROPERTY_RE  
php_1       |   FERENCE_TYPE).                                                               

WedgeSama pushed a commit to WedgeSama/alice that referenced this issue Feb 8, 2023
I thinks the right syntaxe must be something like `@self->created_by->info->created`, but like said it nelmio#1116 and refered in nelmio#1019, it does not work for now. Using this syntax `@self->created_by.info.created`, it works.
theofidry pushed a commit that referenced this issue Feb 11, 2023
I thinks the right syntaxe must be something like `@self->created_by->info->created`, but like said it #1116 and refered in #1019, it does not work for now. Using this syntax `@self->created_by.info.created`, it works.

Co-authored-by: Benjamin Georgeault <benjamin@drosalys.fr>
@abdelazizmirasoft
Copy link

You should try this: @self->city.postalCodes, use the . from the second level to access the sub property.

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

No branches or pull requests

5 participants