2016-11-24 117 views
0

我想在symfony中构建一个搜索过滤器。我正在使用symfony 2.8。我有四个受尊敬的实体的表格。 表是如何根据多个表实体创建一个symfony 2搜索过滤器

professional 
professional_pets 
professional_categories 
professional_organization 

professional_pets表包含professionalIdpetId店面关系的宠物和专业。 同样的方式professional_categories表包含professionalIdcategoryId字段以存储professionalCategories之间的关系。与professional_organization相同的情况。

我想要什么,我想要一个dql查询过滤专业人士形式专业人员表,如果可能的话,只能提取专业表中与professional_pet,professional_categories和professional_organizations具体关系的记录。
在其他工作中,我希望在专业人员表格基础上为专业人士创建过滤器,如果在professional_pet,professional_categories和professional_organizations表格中存在他们的关系sotre。

我附上过滤器的快照。提前感谢您的帮助。 enter image description here

+0

你试过了吗? – Tiger

+0

我没有找到正确的点尝试。我可以把一个函数放在professionalRepository中进行搜索。对我来说唯一的问题是为此编写一个dql。 –

回答

2

编辑:

<?php 

namespace CommonBundle\Entity\Children; 

use Doctrine\ORM\EntityRepository; 
use Doctrine\ORM\QueryBuilder; 


class professionalRepository extends EntityRepository 
{ 

    public function findAllBySearch($searchValues) 
    { 
     /** @var QueryBuilder $query */ 
     //professional entity 
     $query = $this->createQueryBuilder('p'); 

     if(!empty($searchValues['pet_id'])){ 
      $query->join('AppBundle:ProfessionalPets', 'pp', 'WITH', 'pp.professionalId = p.id'); 
      $query->andWhere('pp.petId = :pet_id') 
       ->setParameter('pet_id', $searchValues['pet_id']); 
     } 
     if(!empty($searchValues['cat_id'])){ 
      $query->join('AppBundle:ProfessionalCategories', 'pc', 'WITH', 'pc.professionalId = p.id'); 
      $query->andWhere('pc.categoryId = :cat_id') 
       ->setParameter('cat_id', $searchValues['cat_id']); 
     } 
     if(!empty($searchValues['org_id'])){ 
      $query->join('AppBundle:ProfessionalOrganization', 'po', 'WITH', 'po.professionalId = p.id'); 
      $query->andWhere('po.organizationId = :org_id') 
       ->setParameter('org_id', $searchValues['org_id']); 
     } 

     return $query->getQuery()->getResult(); 
    } 
} 

由于我不知道你的包和实体类名称,您将需要修改它们。

+0

如果你这样做了!empty(...)你不必做array_key_exists(...) - empty()将完成所有的工作。 – Alex

+0

这是相当不错的描述有什么实体参与搜索 - 为什么你把自己的代码没有任何改变?没有改变你的代码是无关紧要的。 – Alex

+0

我对dql没有多少经验。所以实际上我也想知道每个表的查询中引用的位置。怎么运行的。 –