gpt4 book ai didi

php - 多条件搜索查询生成器 [symfony2]

转载 作者:行者123 更新时间:2023-11-28 23:25:56 27 4
gpt4 key购买 nike

我创建了一个搜索表单,我正在尝试从数据库中获取以下数据(airportairport1departurateprice), 我只能得到 [airport] 而不是其他实体。

这是我的 Controller :

public function searchtabflightResultAction(Request $request)
{


$form = $this->createForm(new SearchflightType());

if ($this->get('request')->getMethod() == 'POST')
{
$form->handleRequest($request);

$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form['airport']->getData());

} else {
throw $this->createNotFoundException('The page you were looking for doesn\'t exist.');
}

return $this->render('FLYBookingsBundle:Post:searchtabflightResult.html.twig', array('entities' => $entities));
}

PostRepository.php

public function searchflight($entity)
{
$qb = $this->createQueryBuilder('u')
->select('u')
->where('u.airport like :entity')
->andWhere('u.airport1 like :entity')
->orderBy('u.id')
->setParameter('entity', '%'.$entity.'%');
return $qb->getQuery()->getResult();
}

最佳答案

尝试类似的东西:

在你的 Controller 中:

$entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form);

在你的仓库中:

public function searchflight(FormInterface $form)
{
$qb = $this->createQueryBuilder('u');

if ($form->has('airport')) {
$qb->andWhere(
$qb->expr()->like('u.airport', ':airport')
)
->setParameter('airport', '%'. $form->get('airport')->getData().'%')
}

// ... complete like this with the other params

关于php - 多条件搜索查询生成器 [symfony2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347178/

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