alice icon indicating copy to clipboard operation
alice copied to clipboard

Got array instead of reference

Open alegout opened this issue 5 years ago • 7 comments

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

alegout avatar Feb 19 '20 15:02 alegout

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

AyrtonRicardo avatar Feb 21 '20 10:02 AyrtonRicardo

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.

alegout avatar Feb 24 '20 09:02 alegout

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})>'

AyrtonRicardo avatar Feb 28 '20 16:02 AyrtonRicardo

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 avatar Feb 28 '20 16:02 alegout

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

theofidry avatar Feb 28 '20 19:02 theofidry

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.

stof avatar Apr 29 '24 15:04 stof