首页 > 代码库 > Symfony3 查询搜索功能DQL语句like查询
Symfony3 查询搜索功能DQL语句like查询
//前台页面代码
<form method="GET" action="{{ path(‘staff_index‘) }}" name="searchterm"> <label> <input name="searchterm" type="text" value="" placeholder="输入名称" > </label> <input type="submit" value="查询"> </form>
/** * Lists all customer entities. * * @Route("/", name="staff_index") * @Method("GET") * @Template() */ public function indexAction(Request $request) {
$em = $this->getDoctrine()->getManager();//实体管理 $searchterm = $request->get(‘searchterm‘);//页面参数获取
$page = $request->query->getInt(‘page‘,1); $query = $em->getRepository(‘XinXiBundle:Customer‘)//实体查询 ->createQueryBuilder(‘u‘);
if (!null == $searchterm){//如果参数不为空就执行like查询 $query->where( $query->expr()->like(‘u.name‘, ‘:user‘) ) ->setParameter(‘user‘,‘%‘.$searchterm.‘%‘) ->getQuery() ->getResult(); }else{ $query->orderBy(‘u.id‘,‘desc‘) //否则查询所有列表 ->getQuery(); } $pagination = $this->get(‘knp_paginator‘) //分页功能 ->paginate($query, $page,5); return [ ‘pagination‘ => $pagination, ]; }
多个条件查询时使用 andWhere 如下:
$em = $this->getDoctrine()->getManager(); $searchterm = $request->get(‘searchterm‘); $page = $request->query->getInt(‘page‘,1); $query = $em->getRepository(‘XinXiBundle:Customer‘) ->createQueryBuilder(‘u‘); if (!null == $searchterm){ $query ->andWhere($query->expr()->like(‘u.name‘, ‘:user‘)) ->andWhere(‘u.status = :status‘) ->setParameter(‘status‘,true) ->setParameter(‘user‘,‘%‘.$searchterm.‘%‘) ->getQuery() ->getResult(); }else{ $query->orderBy(‘u.id‘,‘desc‘) ->where(‘u.status = :status‘) ->setParameter(‘status‘,true) ->getQuery(); }
实体关联查询
$em = $this->getDoctrine()->getManager(); $searchterm = $request->get(‘searchterm‘); $page = $request->query->getInt(‘page‘, 1); $query = $em->getRepository(‘XinXiBundle:Finance‘) ->createQueryBuilder(‘f‘); if (!null == $searchterm) { $query ->join(‘f.customer‘ , ‘customer‘) //关联实体属性 现在别名为customer ->andWhere($query->expr()->like(‘customer.name‘, ‘:name‘))//此时再使用customer.属性值进行like查询 ->andWhere(‘f.status = :status‘) ->setParameter(‘status‘, true) ->setParameter(‘name‘, ‘%‘ . $searchterm . ‘%‘) ->orderBy(‘f.id‘, ‘desc‘) ->getQuery() ->getResult();
Symfony3 查询搜索功能DQL语句like查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。