doctrine教程14---where in()

Mario Sanchez

19 people read
Thumbnail

如果我们在搜索时用空格分开关键词,那么我们很可能返回不了我们想搜索的东西,修改一下search方法

public function search(string $term)
    {
        $termList = explode(' ', $term);
        $qb = $this->createQueryBuilder('category')
            ->leftJoin('category.fortuneCookies', 'fortuneCookie')
            ->andWhere('category.name LIKE :searchTerm OR category.name IN(:termList) OR category.iconKey LIKE :searchTerm OR fortuneCookie.fortune LIKE :searchTerm')
            ->setParameter('searchTerm', '%' . $term . '%')
            ->setParameter('termList', $termList);
        $query = $qb->getQuery();
        return $query->getResult();
    }

我们在写原生IN的时候需要用逗号分开并且用括号包裹,但是在Doctrine中只需要传一个数组即可,就是这么简单

About Me

我是一位精通 Symfony 框架和 API Platform 的开发者,擅长构建高效、可扩展的 Web 应用程序和 API。 此外,我还具备 PrestaShop 模块开发经验,能够为您的电商平台定制功能,满足特定业务需求。