我问是否可以将数组作为某些元素的值传递?例如,在我的情况下,我正在尝试为FOSUserBundle用户实体设置角色,该角色将角色作为值数组而不是普通值.我的夹具中有这个:
UserBundle\Entity\User:
User0:
username: admin
email: admin@local.com
enabled: 1
plainPassword: admin
roles: [ROLE_ADMIN]
groups: @Group0
User{1..10}:
username:
email:
enabled:
plainPassword:
roles: 35%? [ROLE_ADMIN, ROLE_USER, ROLE_PROFILE_ONE, ROLE_PROFILE_TWO]
groups: @Group*
但它没有用,我收到了这个错误:
[Symfony\Component\Debug\Exception\ContextErrorException]
Catchable Fatal Error: Argument 1 passed to FOS\UserBundle\Model\User::setRoles() must be of
the type array, string given, 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
对此有何建议?
更新答案
在YAML文件中使用第一种方法使用plain数组:
在做了一些更改之后@frumious建议夹具现在有这样的内容:
UserBundle\Entity\User:
User0:
username: admin
email: admin@local.com
enabled: 1
plainPassword: admin
roles: [ROLE_ADMIN]
groups: @Group0
User{1..10}:
username:
email:
enabled:
plainPassword:
roles: [ROLE_PROFILE_ONE, ROLE_PROFILE_TWO]
groups: @Group*
通过这种方式,我将为每个测试用户分配两个角色,但是我在尝试获取Faker的位置以及在其中编写哪些代码时遇到了一些问题.
但是每当我尝试通过调用来执行set时:
h4cc_alice_fixtures:load:sets ./src/CommonBundle/DataFixtures/TananeSet.php
我收到了这个错误:
[ErrorException] Catchable Fatal Error: Argument 1 passed to
Doctrine\Common\Collections\ArrayCollection::__construct() must be of
the type array, object given, 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
这让我觉得这里的问题与User实体中的$groups变量有关.这是该实体的一段代码:
/**
* @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;
}
我该如何解决这个错误?我应该作为组的参数传递什么?
使用第二种方法:定义服务
按照@frumious的另一个建议,我定义了一个服务如下:
services:
roles.faker.provider:
class: CommonBundle\Tools\RolesFakerProvider
tags:
- { name: h4cc_alice_fixtures.provider }
这是方法:
namespace CommonBundle\Tools;
class RolesFakerProvider {
public function randomRoles()
{
$names = ['ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO'];
return [$names[array_rand($names)]];
}
}
然后我做了这个改变:
UserBundle\Entity\User:
User0:
username: admin
email: admin@local.com
enabled: 1
plainPassword: admin
roles: [ROLE_ADMIN]
groups: @Group0
User{1..10}:
username:
email:
enabled:
plainPassword:
# BEFORE
#roles: [ROLE_PROFILE_ONE, ROLE_PROFILE_TWO]
# AFTER
roles:
groups: @Group*
而这一个正在返回此错误:
[Symfony\Component\Debug\Exception\ContextErrorException] Catchable
Fatal Error: Argument 1 passed to
FOS\UserBundle\Model\User::setRoles() must be of the type array,
string given, called in
/var/www/html/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php
on line 483 and defin ed in
/var/www/html/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Model/User.php
line 530
这让我觉得这个函数没有返回一个数组或其他什么东西出错了,关于这个的任何建议呢?