Symfony查询方法实例小结
程序员文章站
2024-03-11 12:55:31
本文实例讲述了symfony查询方法。分享给大家供大家参考,具体如下:
1. createquery的写法
$sql = 'select count(disti...
本文实例讲述了symfony查询方法。分享给大家供大家参考,具体如下:
1. createquery的写法
$sql = 'select count(distinct(g.goodsid)) from appbundle:goodsindex g where g.status = :status'; $params = array( 'status' => goodsindex::status_normal, ); if (!empty($keywords)) { $params['keywords'] = "%{$keywords}%"; $sql .= ' and g.keywords like :keywords '; } if (!empty($warehouseidlist)) { $params['warehouseidlist'] = $warehouseidlist; $sql .= " and g.warehouseid in :(warehouseidlist)"; } $goodsnum = $this->entitymanager->createquery($sql)->setparameters($params)->getsinglescalarresult();
个人总结::是指占位符的意思,防止sql注入。所以把所有需要的参数做成数组$params里面。
2. getquery的写法
$orderby = 'p.'.$searchoptions['orderby']; $repository = $this->entitymanager ->getrepository('appbundle:goodsindex'); $query = $repository->createquerybuilder('p'); $query->select('distinct(p.goodsid)'); $query->where('p.keywords like :keywords') ->setparameter('keywords', "%{$searchoptions['keywords']}%") ->andwhere('p.status = :status') ->setparameter('status', goodsindex::status_normal) ->orderby($orderby, $searchoptions['order']) ->setfirstresult($pagination['pagesize'] * ($pagination['page'] - 1)) ->setmaxresults($pagination['pagesize']); if (!empty($searchoptions['warehouseidlist'])) { $query->andwhere($query->expr()->in('p.warehouseid', $searchoptions['warehouseidlist'])); } $goodsindexlist = $query->getquery()->getresult();
更多关于symfony相关内容感兴趣的读者可查看本站专题:《symfony框架入门教程》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于symfony2框架的php程序设计有所帮助。