首页 > 解决方案 > 带有教义“错误:无效的 PathExpression。必须是 StateFieldPathExpression”的正则表达式。

问题描述

我有一个$value包含“打开|关闭”的变量。我想在 Symfony 中使用学说查询来获取所有带有“打开”或“关闭”一词的“状态”。为此,我尝试使用正则表达式。但是,我收到以下错误:

[语义错误] line 0, col 75 near 'status, :regexp)': Error:
Invalid PathExpression。必须是 StateFieldPathExpression。

这是我的代码:

$qb = $this->createQueryBuilder('q')
->andWhere('REGEXP(q.status, :regexp) = true')
->setParameter('regexp', '|');

我有DoctrineExtensionsBundle并且我已经更新了我的app/config.yml

标签: regexsymfonydoctrine-ormdoctrinedoctrine-query

解决方案


使用explode() 函数和SQL IN 运算符怎么样?

$qb = $this->createQueryBuilder('q')
  ->andWhere('q.status IN (:status)')
  ->setParameter(':status', explode('|', $value));

它将返回在状态列中具有“打开”或“关闭”的所有记录。


推荐阅读