【问题标题】:Symfony - persisting array in string fieldSymfony - 字符串字段中的持久数组
【发布时间】:2021-10-30 18:15:33
【问题描述】:

在我的 Symofny 项目中,在持久化实体时,我通过请求传递我的用户角色,当转储该角色时,结构如下所示:

roles dump img

归档我将它持久化,(不在用户表中)用于一些混合值,它是 string 的类型,因为我将它用于混合目的。 所以,当坚持我总是得到:

注意:数组到字符串的转换

我发现了很多问题,但没有一个对我有用:

我很害怕:

$new->setValue(json_encode(json_decode($roles)));

$new->setValue(implode(',', $roles));

总是遇到同样的错误。有人可以提供替代方法,以便我可以将角色持久保存到字符串字段中并克服此错误吗?

【问题讨论】:

标签: php symfony doctrine-orm symfony4


【解决方案1】:

我猜 JSON 编码是由 Doctrine 完成的。通常,我会这样设置角色:

public function setRoles(array $roles): self
{
    $this->roles = $roles;

    return $this;
}

public function addRole(string $role): self
{
    $this->roles[] = $role;

    return $this;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多