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

Got array instead of reference #1018

Open
alegout opened this issue Feb 19, 2020 · 7 comments
Open

Got array instead of reference #1018

alegout opened this issue Feb 19, 2020 · 7 comments

Comments

@alegout
Copy link

alegout commented Feb 19, 2020

Hello,

https://github.com/nelmio/alice/blob/v3.5.8/doc/advanced-guide.md#references

@user_{alice, bob} => @user_alice or @user_bob

If i:

client_type:

AppBundle\Entity\ClientType:
    client_type_{1..10}:
        name: '<name()>'
include:
    - client_type.yaml

AppBundle\Entity\Client:
    client_{1..20}:
        name: '<name()>'
        client_type: '@client_type_{1..10}'

I got:

Expected argument of type "AppBundle\Entity\ClientType or null", "array" given

It gives me an array with the 10 ClientType for the client_type field.

Is it normal?

Thank you

@AyrtonRicardo
Copy link

AyrtonRicardo commented Feb 21, 2020

Which version are you using?
I think you are receiving a list of AppBundle\Entity\ClientType when you request using {1..10} operator.

You have option to do like this:

include:
    - client_type.yaml

AppBundle\Entity\Client:
    client_{1..20}:
        name: '<name()>'
        client_type: '<randomElement(@client_type_{1..10})>'

or simply like this:

include:
    - client_type.yaml

AppBundle\Entity\Client:
    client_{1..20}:
        name: '<name()>'
        client_type: '@client_type_*'

In the docs you provided it only says how to use the reference of the object you've created, in this case:

# this will create a collection of ClientType named as `client_type_` from 1 to 10
AppBundle\Entity\ClientType:
    client_type_{1..10}:
        name: '<name()>'

you can use its reference either:

  • @client_type_1 or @client_type_2 => single instance
  • @client_type_* => single instance
  • @client_type_{1..10} => the whole collection you've created

@alegout
Copy link
Author

alegout commented Feb 24, 2020

Thank you for your help. I am using the version 3.5.8.

With @client_type_{1, 2, 3} i should have @client_type_1 or @client_type_2 or @client_type_3 but it gives me an array with the three, sounds like a bug to me.

@AyrtonRicardo
Copy link

Actually whenever you use the operator {} you automatically is creating a collection.
In you creation you define @client_type_{1, 2, 3} => then it's available to use @client_type_1 or @client_type_2 or @client_type_3.
The operator to get a random one is @client_type_*

To solve your problem you can follow the comment above it will be fine for you.

include:
    - client_type.yaml

AppBundle\Entity\Client:
    client_{1..20}:
        name: '<name()>'
        client_type: '<randomElement(@client_type_{1..10})>'

@alegout
Copy link
Author

alegout commented Feb 28, 2020

I am using the randomElement feature, It is just to ask if the doc is wrong or not and to report it if it is the case.

Because of @user_{alice, bob} => @user_alice or @user_bob in https://github.com/nelmio/alice/blob/v3.5.8/doc/advanced-guide.md#references

Closing here, thank you for your help.

@alegout alegout closed this as completed Feb 28, 2020
@theofidry
Copy link
Member

Let's check if that's a bug or the doc that is not correct :)

@theofidry theofidry reopened this Feb 28, 2020
@theofidry theofidry added this to the 3.x milestone Feb 28, 2020
@stof
Copy link
Contributor

stof commented Apr 29, 2024

To me, this is a documentation issue. It should say and, not or.
https://github.com/nelmio/alice/blob/v3.11.0/doc/relations-handling.md#multiple-references documents this syntax as producing an array.

@Seros
Copy link

Seros commented Oct 11, 2024

I just face a similar issue when trying to receive one single entity but get an array of entities when declaring @user_{alice, bob}. So I found this issue and tried to apply the suggested workaround <randomElement(@user_{alice, bob})> but this throws an exception:

Nelmio\Alice\Throwable\Exception\Generator\DebugUnexpectedValueException : An error occurred while generating the fixture ...: Could not resolve value during the generation process.

I checked and it basically tries to resolve user_ instead of one of the requested identifiers. The internal exception is from vendor/nelmio/alice/src/Throwable/Exception/Generator/Resolver/FixtureNotFoundExceptionFactory.php:
Could not find the fixture "user_".
Does anyone know a workaround for this or is this rather a bug/race condition in resolving the references and calling the function randomElement?

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

5 participants