《PHP学习:Symfony查询方法实例小结》要点:
本文介绍了PHP学习:Symfony查询方法实例小结,希望对您有用。如果有疑问,可以联系我们。
PHP实例本文实例讲述了Symfony查询方法.分享给大家供大家参考,具体如下:
PHP实例1. createQuery的写法
PHP实例
$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();
PHP实例个人总结::是指占位符的意思,防止sql注入.所以把所有需要的参数做成数组$params里面.
PHP实例2. getQuery的写法
PHP实例
$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();
PHP实例更多关于Symfony相关内容感兴趣的读者可查看本站专题:《Symfony框架入门教程》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
PHP实例希望本文所述对大家基于Symfony2框架的PHP程序设计有所帮助.
转载请注明本页网址:
http://www.vephp.com/jiaocheng/562.html