I ask, is it possible to pass an array as a value for some elements? For example, in my case, I am trying to set roles for the FOSUserBundle User object, which takes roles as an array of values, not simple values. I have this in my chair:
UserBundle\Entity\User: User0: username: admin email: admin@local.com enabled: 1 plainPassword: admin roles: [ROLE_ADMIN] groups: @Group0 User{1..10}: username: <firstNameMale> email: <companyEmail> enabled: <boolean(35)> plainPassword: <lexify> roles: 35%? [ROLE_ADMIN, ROLE_USER, ROLE_PROFILE_ONE, ROLE_PROFILE_TWO] groups: @Group*
But it does not work, and I get this error:
[Symfony \ Component \ Debug \ Exception \ ContextErrorException] Catchable Fatal Error: argument 1 passed to FOS \ UserBundle \ Model \ User :: setRoles () must be from an array of types, the specified string, called / var / www / html / vendor / nelmio / alice / src / Nelmio / Alice / Loader / Base.php on line 483 and defined in / var / www / html / vendor / friendsofsymfony / user-bundle / FOS / UserBundle / Model / User.php line 530
Any advice on this?
Update Answer
Using the first approach with a simple array in a YAML file:
After making some changes, as @frumious suggested that the device now has this content:
UserBundle\Entity\User: User0: username: admin email: admin@local.com enabled: 1 plainPassword: admin roles: [ROLE_ADMIN] groups: @Group0 User{1..10}: username: <firstNameMale> email: <companyEmail> enabled: <boolean(35)> plainPassword: <lexify> roles: [ROLE_PROFILE_ONE, ROLE_PROFILE_TWO] groups: @Group*
Thus, I will always assign two roles for each test user, but I have some problems trying to find the place where Faker should be placed, and what code to write inside it.
But anytime I try to execute a set by calling:
h4cc_alice_fixtures:load:sets ./src/CommonBundle/DataFixtures/TananeSet.php
I got this error:
[ErrorException] Catchable Fatal Error: argument 1 passed to Doctrine \ Common \ Collections \ ArrayCollection :: __ construct () must be from an array of types, a given object, called in / var / www / html / vendor / doctrine / orm / lib / Doctrine /ORM/UnitOfWork.php o
n line 555 and defined in /var/www/html/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php line 47
Which makes me think that the problem here is related to the $groups variable in the User object. This is the code snippet for this object:
/** * @ORM\Entity * @ORM\Table(name="fos_user") * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false) * @ORM\Entity(repositoryClass="UserBundle\Entity\Repository\UserRepository") */ class User extends BaseUser { /** * Hook timestampable behavior * updates createdAt, updatedAt fields */ use TimestampableEntity; /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToMany(targetEntity="Group") * @ORM\JoinTable(name="fos_user_user_group", * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} * ) */ protected $groups; /** * @ORM\Column(name="deletedAt", type="datetime", nullable=true) */ protected $deletedAt; }
How can I fix this error? What should I pass as a parameter to groups ?
Using the second approach: service definition
Following another @frumious suggestion, I define the following service:
services: roles.faker.provider: class: CommonBundle\Tools\RolesFakerProvider tags: - { name: h4cc_alice_fixtures.provider }
And this is the method:
namespace CommonBundle\Tools; class RolesFakerProvider { public function randomRoles() { $names = ['ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO']; return [$names[array_rand($names)]]; } }
Then I made the following changes:
UserBundle\Entity\User: User0: username: admin email: admin@local.com enabled: 1 plainPassword: admin roles: [ROLE_ADMIN] groups: @Group0 User{1..10}: username: <firstNameMale> email: <companyEmail> enabled: <boolean(35)> plainPassword: <lexify> # BEFORE #roles: [ROLE_PROFILE_ONE, ROLE_PROFILE_TWO] # AFTER roles: <randomRoles> groups: @Group*
And instead, this error is returned:
[Symfony \ Component \ Debug \ Exception \ ContextErrorException] Grip Fatal error: argument 1 passed to FOS \ UserBundle \ Model \ User :: setRoles () must be an array of types, a line called in / var / www / html / vendor / nelmio / alice / src / Nelmio / Alice / Loader / Base.php on line 483 and defined in / var / www / html / vendor / friendsofsymfony / user-bundle / FOS / UserBundle / Model / User.php line 530
What makes me think that the function is not returning an array , or something else is failing, any advice around this too?