[Symfony] On using the $current variable with nelmio/alice

Published on 2021-07-10 • Modified on 2021-07-10

In this snippet, we see how to use the $current variable with the nelmio/alice fixtures library. $current is one the most helpful variable as it stores the index declared in the fixture item being processed; for example, organization_dummy_{4}. In this case, $current evaluates to four. I put several examples with dummy corporations I create when loading this blog's fixtures; you have below the actual output for the name field 😉.


App\Entity\Organization:
  organization_strangebuzz:
    name: Strangebuzz
    logo: '@image_object_strangebuzz'

  organization_dummy_{2}: # explicit usage of the identity provider
    name: Dummy Corp <identity($current + 2021)>

  organization_dummy_{3}: # implicit usage of the identity provider
    name: Dummy Corp <($current + 2000)>

  organization_dummy_{4}: # direct usage of the $current variable
    name: Dummy Corp $current

  organization_dummy_{5}: # direct usage of the $current with simple quotes
    name: 'Dummy Corp $current'

  organization_dummy_{6}: # usage of current with its provider
    name: Dummy Corp <current()>

  organization_dummy_{7}: # usage of current variable combined with other valid PHP code
    name: Dummy Corp <($current + random_int(2000, 2021))>

  organization_dummy_{8}: # Current is escaped so it's not replaced the value
    name: Dummy Corp \$current

  # Produces :
  # 1: Strangebuzz
  # 2: Dummy Corp 2023
  # 3: Dummy Corp 2003
  # 4: Dummy Corp 4
  # 5: Dummy Corp 5
  # 6: Dummy Corp 6
  # 7: Dummy Corp 2019
  # 8: Dummy Corp $current

 More on Stackoverflow   Read the doc  Random snippet