【问题标题】:Symfony Doctrine - select users with specific roleSymfony Doctrine - 选择具有特定角色的用户
【发布时间】:2020-04-22 17:45:48
【问题描述】:

我正在尝试从 SQLite 数据库(Doctrine)中选择用户,但我只想选择具有 ROLE_ADMIN 的教师,但它不起作用,我收到错误。

这里是查询代码:

$teachers = $em->createQueryBuilder()
     ->select('t.username, t.firstName as firstname, t.surname, t.email, t.id')
     ->from('App:User\User', 't')
     ->where('t.roles LIKE :role')
     ->setParameter('role', '%"ROLE_ADMIN"%')
     ->getQuery()
     ->getResult();

我得到这个错误:

[语义错误] 第 0 行,第 99 列靠近 'roles LIKE :':错误:无效 路径表达式。必须是 StateFieldPathExpression。

你能帮忙吗?

【问题讨论】:

  • 你为什么有这个->from('App:User\User', 't')?试试->from(User::class, 't')
  • 您能发布您的roles 映射吗?
  • $role = 'ROLE_ADMIN'; $teachers = $em->createQueryBuilder() ->select('t.username, t.firstName as firstname, t.surname, t.email, t.id') ->from('App:User\User', 't') ->where('t.roles LIKE :role') ->setParameter('role', '%'.$role .'%') ->getQuery() ->getResult();

标签: php symfony doctrine doctrine-query symfony5


【解决方案1】:

去掉双引号

->setParameter('role', '%ROLE_ADMIN%')

【讨论】:

  • 不起作用,仍然是同样的错误:[Semantical Error] line 0, col 99 near 'roles LIKE :': Error: Invalid PathExpression。必须是 StateFieldPathExpression。
  • 我测试了建议的修复程序并且它有效。缓存问题?
猜你喜欢
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 2017-11-02
  • 2014-08-31
相关资源
最近更新 更多