gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:39:32 26 4
gpt4 key购买 nike

我想通过“名字”和“姓氏”搜索用户

所以一开始我有一个输入文本,我把姓氏和名字放在同一个输入中

然后我创建了这个查询

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 = "test" 和 lastname="test2"

如果我输入这个词:“test”或“test2”它的工作并返回用户

如果我说:"test test2"是行不通的(没有结果)

请问谁有办法解决这个问题

最佳答案

可能是因为你使用了两次参数所以尝试分配分配两个参数

例如:这样试试

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();
}

关于mysql - 我在查询生成器 SYMFONY 中遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55790824/

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