自定义数据库结果 [Symfony 4]
Customize database result [Symfony 4]
我喜欢一个搜索表单
例如,我想按类别搜索,文凭,薪水,
大类,文凭不错
但我的问题是在salaire(类型float)中,当salaire null形式我想要查询return类别的所有结果,文凭并忽略salaire
当表格salaire不为空时,我想要查询return categorie+diplome+salaire list
的结果
这是控制器
$data = $form->getData();
$repository = $this->getDoctrine()->getRepository(Candidat::class);
$candidats = $repository->findListByFilter($data->getCategorie(),$data->getDiplome(),$data->getSalaire());
这是存储库
public function findListByFilter($categorie,$diplome,$salaire):array
{
return $this->createQueryBuilder('c')
->andWhere('c.categorie = :categorie')
->andWhere('c.diplome = :diplome')
->andWhere('c.salaire <= :salaire')
->setParameter('categorie', $categorie)
->setParameter('diplome', $diplome)
->setParameter('salaire', $salaire)
->getQuery()
->getResult()
;
}
我推荐你使用IFs
来完成这种请求:
public function findListByFilter(string $categorie, string $diplome, ?float $salaire = null): array
{
$qb = $this->createQueryBuilder('c')
->where('c.categorie = :categorie')
->andWhere('c.diplome = :diplome')
->setParameters([
'categorie' => $categorie,
'diplome' => $diplome
]);
if($salaire) {
$qb->andWhere('c.salaire <= :salaire')->setParameter('salaire', $salaire);
}
return $qb->getQuery->getResult();
}
您使用的是 Symfony 4,因此我建议您开始对变量使用类型提示。
我喜欢一个搜索表单 例如,我想按类别搜索,文凭,薪水,
大类,文凭不错 但我的问题是在salaire(类型float)中,当salaire null形式我想要查询return类别的所有结果,文凭并忽略salaire
当表格salaire不为空时,我想要查询return categorie+diplome+salaire list
的结果这是控制器
$data = $form->getData();
$repository = $this->getDoctrine()->getRepository(Candidat::class);
$candidats = $repository->findListByFilter($data->getCategorie(),$data->getDiplome(),$data->getSalaire());
这是存储库
public function findListByFilter($categorie,$diplome,$salaire):array
{
return $this->createQueryBuilder('c')
->andWhere('c.categorie = :categorie')
->andWhere('c.diplome = :diplome')
->andWhere('c.salaire <= :salaire')
->setParameter('categorie', $categorie)
->setParameter('diplome', $diplome)
->setParameter('salaire', $salaire)
->getQuery()
->getResult()
;
}
我推荐你使用IFs
来完成这种请求:
public function findListByFilter(string $categorie, string $diplome, ?float $salaire = null): array
{
$qb = $this->createQueryBuilder('c')
->where('c.categorie = :categorie')
->andWhere('c.diplome = :diplome')
->setParameters([
'categorie' => $categorie,
'diplome' => $diplome
]);
if($salaire) {
$qb->andWhere('c.salaire <= :salaire')->setParameter('salaire', $salaire);
}
return $qb->getQuery->getResult();
}
您使用的是 Symfony 4,因此我建议您开始对变量使用类型提示。