脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Symfony2实现搜索过滤器表单,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在与实体关联的存储库上使用特定方法来在查询上添加联接(以避免许多查询).控制器看起来像这样:
class ModelController extends Controller { public function indexAction(Request $request) { // ... $em = $this->getDoctrine()->getManager(); $query = $em->getReposITory('AcmeDemoBundle:Item')->getList(); } }
存储库上的getList方法如下所示:
use Doctrine\ORM\EntityRepository; // ... class ItemRepository extends EntityRepository { public function getList() { $queryBuilder = $this ->createQueryBuilder('i') ->innerJoin('i.brand','b'); return $queryBuilder->getQuery(); } }
我创建了一个ItemSeArchTyPE表单对象,其中包含几个字段来搜索项目.
如何从搜索表单中提供的数据轻松添加搜索条件以显示已过滤的项目?
class ModelController extends Controller { public function indexAction(Request $request) { // ... if ($request->getmethod() === 'POST') { $searchForm->bindRequest($request); if ($searchForm->isValid()) { $searchCriteria = $searchForm->getData(); // Do something with this data! ...but I don't kNow how } } }
谢谢!
public function getListBy($criteria) { $qb = $this->createQueryBuilder('i'); $qb->innerJoin('i.brand','b'); foreach ($criteria as $field => $value) { if (!$this->getClassMetadata()->hasField($field)) { // Make sure we only use existing fields (avoid any injection) continue; } $qb ->andWhere($qb->exPR()->eq('i.'.$field,':i_'.$field)) ->setParameter('i_'.$field,$value); } return $qb->getQuery()->getResult(); }
以上是脚本宝典为你收集整理的php – 如何使用Symfony2实现搜索过滤器表单全部内容,希望文章能够帮你解决php – 如何使用Symfony2实现搜索过滤器表单所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。