drpfu51608120170 2016-11-24 06:36
浏览 41
已采纳

如何基于多个表实体创建symfony 2搜索过滤器

I want to build a search filter in symfony. I am using symfony 2.8. I have four table with respected entities. Table are

professional
professional_pets
professional_categories
professional_organization

professional_pets table contain professionalId and petId for store the relation pet and professional. same way the professional_categories table contains professionalId and categoryId fields to store relation between professional and Categories. Same case with professional_organization.

What I want that I want a dql query to filter professionals form professionals table if it is possible which can extract only those records from professional table which have relation with specific in professional_pet, professional_categories and professional_organizations.
In other work I want tho create filter for professinals in professionals table base on the if there is a relation sotre for them in professional_pet, professional_categories and professional_organizations tables.

I am attaching a snapshot of filter. Thanks for help in advance. enter image description here

  • 写回答

1条回答 默认 最新

  • dongzhuohan7085 2016-11-24 07:21
    关注

    Edit:

    <?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();
        }
    }
    

    Since I don't know what your bundles and entity classes names are, you will need to modify them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办