当前位置:Gxlcms > PHP教程 > Symfony查询方法小结分享

Symfony查询方法小结分享

时间:2021-07-01 10:21:17 帮助过:38人阅读

本文主要和大家介绍了Symfony查询方法,结合实例形式总结分析了createQuery与getQuery查询数据的具体使用技巧,需要的朋友可以参考下,希望能帮助到大家。

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针对输入时间进行查询实例详解

详解Symfony2框架表单的用法

详解Symfony2实现联合查询的方法

以上就是Symfony查询方法小结分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行