【问题标题】:Find the result if the field text contains specific string如果字段文本包含特定字符串,则查找结果
【发布时间】:2022-08-02 16:38:31
【问题描述】:

我正在做一个 symfony(4.4) 项目。我正在尝试使用教义进行查询,以匹配文本包含数组中定义的任何字符串的所有结果。

单词的数组是:

$keywords = [
\'test\',
\'test2\',
\'test3\'
];

我试图这样做的查询是:

$qb = $this->createQueryBuilder(\'of\');
foreach ($keywords as $key => $keyword) {
    $qb
        ->orWhere(\'of.text LIKE :keyword\'.$key)
        ->setParameter(\'keyword\'.$key, \'%\'.$keyword.\'%\');
}

当我执行查询时出现错误:

[Syntax Error] line 0, col 7: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | \"(\" Subselect \")\" | CaseExpression, got \'of\'

我正在使用 orWhere,因为我需要匹配这些值中的任何一个,而不是全部(这就是我不使用 andWhere 的原因)。我错过了什么?

  • 也许你错过了$qb->select(\'of\')
  • 我很早就试过了,但我仍然得到同样的错误。
  • 也许 \'of\' 是保留关键字?你试试 $this->createQueryBuilder(\'of\') 吗?

标签: php doctrine-orm symfony4


【解决方案1】:

我认为存在语法错误(即'of.text LIKE:keyword'.$key)。也许'of'是一个保留关键字(https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words)。

你可以试试:

$qb = $this->createQueryBuilder('`of`');
foreach ($keywords as $keyword) {
    $qb->orWhere('of.text LIKE :searchTerm')
    $qb->setParameter('searchTerm', '%'.$keyword.'%');
}

【讨论】:

  • 我试过了,但同样的错误:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多