本文实例讲述了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();
希望本文所述对大家基于Symfony2框架的PHP程序设计有所帮助。
本文Symfony查询方法实例小结到此结束。人生的价值并不在于成功后的荣光,而在于追求的本身,在于信念的树立与坚持的过程。坚守信念,犹如在内心撒下一颗种子,只要在适宜的条件下,种子自会生根发芽破土而出,总会有收获果实的期望。有时需要外力辅助才可取得成果,但最终还要靠自我去完成,因为任何人也不可能把信念深植于你的心中。所以,我们要坚守自我的信念,播下期望的种子。小编再次感谢大家对我们的支持!