Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony/条令:persistentCollection错误_Php_Symfony_Twig - Fatal编程技术网

Php Symfony/条令:persistentCollection错误

Php Symfony/条令:persistentCollection错误,php,symfony,twig,Php,Symfony,Twig,在细枝上显示表格时出现问题。让我解释一下,我有两个用户和角色实体,它们通过多个关系链接,所以我有一个用户角色透视表。问题是,当我想在一个表中显示用户表及其角色时,会出现以下错误: 在呈现模板期间引发了异常 (“可捕获致命错误:类的对象 无法将条令\ORM\PersistentCollection转换为字符串“” 下面是User.php的代码 class User implements UserInterface, \Serializable { /** * @ORM\Id() * @ORM\Ge

在细枝上显示表格时出现问题。让我解释一下,我有两个用户和角色实体,它们通过多个关系链接,所以我有一个用户角色透视表。问题是,当我想在一个表中显示用户表及其角色时,会出现以下错误:

在呈现模板期间引发了异常 (“可捕获致命错误:类的对象 无法将条令\ORM\PersistentCollection转换为字符串“”

下面是User.php的代码

class User implements UserInterface, \Serializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;

/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;

/**
* @ORM\Column(type="boolean", nullable=false, options={"default" : 0})
*/
private $isActive;

/**
* @ORM\Column(type="string", unique=true, length=64)
*/
private $token;

/**
* @ORM\Column(type="datetime")
*/
private $expiresAt;

/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", fetch="LAZY")
* @ORM\JoinTable(name="user_role",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
* }
* )
*
*/
private $roles = [];

public function __construct()
{
$this->roles = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string)$this->username;
}

public function setUsername(string $username): self
{
$this->username = $username;

return $this;
}

/**
* @inheritDoc
*/
public function getRoles(): array //array return
{
$roles[] = $this->roles;
var_dump($roles);

// Afin d'être sûr qu'un user a toujours au moins 1 rôle
if (empty($roles)) {
$roles[] = 'ROLE_ADMIN';
}

return $roles;
}


/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string)$this->password;
}

public function setPassword(string $password): self
{
$this->password = $password;

return $this;
}

/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}

/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}

public function getIsActive(): ?bool
{
return $this->isActive;
}

public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;

return $this;
}

public function getToken(): ?string
{
return $this->token;
}

public function setToken(string $token): self
{
$this->token = $token;

return $this;
}

public function getExpiresAt(): ?\DateTimeInterface
{
return $this->expiresAt;
}

public function setExpiresAt(\DateTimeInterface $expiresAt): self
{
$this->expiresAt = $expiresAt;

return $this;
}

public function isExpired(): bool
{
return $this->getExpiresAt() <= new \DateTime();
}

public function createToken()
{
return substr(str_replace(['+', '/'], ['-', '_'], base64_encode(random_bytes(50))), 0, 63);
}

public function setRoles($roles)
{
if (is_array($roles)) {
$this->roles = $roles;
} else {
$this->roles->clear();
$this->roles->add($roles);
}
return $this;
}

/**
* Add userRoles
*
* @param \App\Entity\Role $roles
* @return User
*/
public function addRoles(\App\Entity\Role $roles)
{
$this->roles[] = $roles;

return $this;
}


/**
* Remove userRoles
*
* @param \App\Entity\Role $roles
*/
public function removeRoles(\App\Entity\Role $roles)
{
$this->roles->removeElement($roles);
}

/**
* String representation of object
* @link https://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* Transform object to string
*/
public function serialize()
{
return serialize([
$this->id,
$this->username,
$this->password,
$this->isActive,
$this->token,
$this->expiresAt,
$this->roles
]);
}

/**
* @param string $serialized
* Transform string to object
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->username,
$this->password,
$this->isActive,
$this->token,
$this->expiresAt
) = unserialize($serialized, ['allowed_classes' => false]);
}
然后在我的小枝中,我得到数组中的数据

{% for list in user %}
   <TR>
<TD>{{ list.id }}</TD>
<TD>{{ list.username }}</TD>
<TD>{{ list.password }}</TD>
<TD>
{% for role in list.roles %}
               {{ role }}
           {% endfor %}

       </TD>
<TD>{{ list.isActive }}</TD>

<TD>
<a href="{{ path('admin_user_edit', {id: list.id}) }}" class="btn btn-secondary">Editer</a>
<form method="post" action="{{ path('admin_user_delete', {id: list.id}) }}" style="display: inline-block"
onsubmit="return confirm('Voulez-vous vraiment supprimer l\'utilisateur ?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ list.id) }}">
<button class="btn btn-primary">Supprimer</button>
</form>
</TD>
</TR>
{% endfor %}
{%用于用户%中的列表]
{{list.id}
{{list.username}
{{list.password}}
{list.roles%中角色的%s}
{{role}}
{%endfor%}
{{list.isActive}
供给者
{%endfor%}
我想知道如何查找角色数据而不是persistentCollection


如果你们中有人能启发我,请提前感谢。

您忘记了用户实体的反面:

//User.php
/**
*@ORM\ManyToMany(targetEntity=“Role”,inversedBy=“users”)
*/
$角色;
公共函数构造()
{
此->角色=新建ArrayCollection();
}
//生成Getter和Setter
... 

可能将
{{role}}
替换为
{role.name}}
我已经尝试过了,这告诉我它不存在什么不存在?错误:属性“name”或方法“name()”、“getname()”/“isname()”/“hasname()”或“\uu call()”都不存在,并且在类“Doctrine\ORM\PersistentCollection”中具有公共访问权限。错误来自“$roles[]=$this->roles;”它需要是“$roles=$this->roles;”对不起,我忘了什么?
{% for list in user %}
   <TR>
<TD>{{ list.id }}</TD>
<TD>{{ list.username }}</TD>
<TD>{{ list.password }}</TD>
<TD>
{% for role in list.roles %}
               {{ role }}
           {% endfor %}

       </TD>
<TD>{{ list.isActive }}</TD>

<TD>
<a href="{{ path('admin_user_edit', {id: list.id}) }}" class="btn btn-secondary">Editer</a>
<form method="post" action="{{ path('admin_user_delete', {id: list.id}) }}" style="display: inline-block"
onsubmit="return confirm('Voulez-vous vraiment supprimer l\'utilisateur ?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ list.id) }}">
<button class="btn btn-primary">Supprimer</button>
</form>
</TD>
</TR>
{% endfor %}