gpt4 book ai didi

php - mariadb 中长文本中的 JSON 返回 "Could not convert database value to Doctrine type json"

转载 作者:行者123 更新时间:2023-12-04 14:58:01 27 4
gpt4 key购买 nike

Symfony 5,我写我的 User相应的实体代码:
https://symfony.com/doc/current/security.html#denying-access-roles-and-other-authorization
我使用 mariadb

public function getRoles(): array
{
$roles[] = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';

return array_unique($roles);
}
我手动添加了一个角色为 ROLE_ADMIN 的用户.
我不明白为什么它会返回错误:

Could not convert database value "ROLE_ADMIN" to Doctrine Type json

最佳答案

您收到该错误是因为您将值设置为 无效的 JSON .如果要直接修改数据库,则应为 JSON 类型输入有效值。在这种情况下,它将类似于:

["ROLE_ADMIN"]
代替:
ROLE_ADMIN
此外,您选择了错误的列类型。虽然 JSONLONGTEXT 的别名在 MariaDb,在 JSON类型化的列添加了一个约束。如您链接的文档中所述:

In order to ensure that a a valid json document is inserted, the JSON_VALID function can be used as a CHECK constraint. This constraint is automatically included for types using the JSON alias from MariaDB 10.4.3.


这会提醒您手动输入的数据不是有效的 JSON 文档。
绝对不需要更改默认值 getRoles()你展示的方法。如果您使用 JSON 类型的属性作为错误显示,那将工作得很好。

关于php - mariadb 中长文本中的 JSON 返回 "Could not convert database value to Doctrine type json",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67573037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com