Doctrine Symfony 查找是否在带有数组的数据库中
我是一名学生,对于我的期末项目,我需要制作一个搜索工具。
我知道如何搜索数据库中是否包含字符串,但我现在想使用数组进行搜索。所以我这样做了,这就是工作......但前提是它正是我的数组中的内容,而不是它包含的内容。
所以我的存储库函数是这样的
public function findByMots($value)
{
return $this->createQueryBuilder('a')
->andWhere('a.titre IN (:val) OR a.contenu IN (:val) OR t.theme IN (:val)')
->setParameter('val', $value)
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id')
->orderBy('a.id', 'DESC')
->getQuery()
->getResult()
;
}
我的值是一个包含字符串的数组,但是如果一个字符串是“浣熊”,他找不到我的文章标题为“我爱浣熊”。请问有谁知道吗?
我找到了方法!
public function findByMots($value)
{
$search = $this->createQueryBuilder('a')
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id');
$number = 0;
foreach ($value as $valu) {
$search = $search->orWhere('a.titre LIKE :val' . $number . ' OR a.contenu LIKE :val' . $number . ' OR t.theme LIKE :val' . $number . '')
->setParameter('val'.$number, '%'.$valu.'%');
$number++;
}
$search = $search
->orderBy('a.id', 'DESC')
->getQuery()
->getResult();
return $search;
}
I'm a student and, for my final project, I need to make a searchtool.
I know how to search if a string is contained in my database but I want to search with an array now. So I do it and that's work... but only if it's exactly what it's in my array, not if it's contained.
So my Repository function is like this
public function findByMots($value)
{
return $this->createQueryBuilder('a')
->andWhere('a.titre IN (:val) OR a.contenu IN (:val) OR t.theme IN (:val)')
->setParameter('val', $value)
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id')
->orderBy('a.id', 'DESC')
->getQuery()
->getResult()
;
}
My value is an array with string in, but if one string is "raccoon" he doesn't find my article who has a title like "I love raccoon". Does anybody know please?
I found a method!
public function findByMots($value)
{
$search = $this->createQueryBuilder('a')
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id');
$number = 0;
foreach ($value as $valu) {
$search = $search->orWhere('a.titre LIKE :val' . $number . ' OR a.contenu LIKE :val' . $number . ' OR t.theme LIKE :val' . $number . '')
->setParameter('val'.$number, '%'.$valu.'%');
$number++;
}
$search = $search
->orderBy('a.id', 'DESC')
->getQuery()
->getResult();
return $search;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论