我在查询生成器SYMFONY中遇到问题

时间:2019-04-22 07:35:37

标签: mysql symfony doctrine repository query-builder

我要按“名字”和“姓氏”搜索用户

所以起初我有一个输入文本,我将姓和名放在同一输入中

第二次我创建了这个查询

public function findListByName($name)
{
    return $this->createQueryBuilder('c')
        ->where('c.firstname LIKE :name')
        ->orWhere('c.lastname LIKE :name)
        ->setParameter('name', '%'.$name.'%')
        ->getQuery()
        ->getResult();
}

更新为:

return $this->createQueryBuilder('c')
        ->where('c.firstname LIKE :name1')
        ->orWhere('c.lastname LIKE :name2')
        ->setParameter(':name1', '%'.$name.'%')
        ->setParameter('name2', '%'.$name.'%')
        ->getQuery()
        ->getResult();

例如: 我在数据库中有firstname = “测试” 和lastname = “ test2”

如果我输入以下单词:“ test”或“ test2”,则返回用户

如果我输入:“ test test2 ”不起作用(无结果)

所以有什么解决方案的人请

2 个答案:

答案 0 :(得分:1)

可能是因为您使用了两次参数,所以请尝试为assign指定两个参数

例如:尝试这种方式

public function findListByName($name)
{
      return $this->createQueryBuilder('c')
        ->where('c.firstname LIKE concat('%',:name1', '%') )
        ->orWhere('c.lastname LIKE concat('%',:name2', '%') )
        ->orWhere('concat(c.firstname,' ',c.lastname) LIKE concat('%',:name3', '%') )
        ->setParameters(array('name1'=> $name, 'name2' =>$name, 'name3'=>$name)
        ->getQuery()
        ->getResult();
}

答案 1 :(得分:0)

您可以检查$ name是否有空格

Dim i As Long, LastRow As Long
Dim WordsArr As Variant

' loop through rows
For i = 1 To LastRow
    WordsArr = Split(Range("D" & i).Value, " ")  ' use Split and space to read cell words to array

    If UBound(WordsArr) >= 1 Then ' make sure the cell contents is at least 2 words
        Range("E" & i).Value = WordsArr(0) & " " & WordsArr(1) ' insert only the first 2 words

    Else ' in case there are less than 2 words
        ' do someting....

    End If
Next i

End Sub