【问题标题】:how to group the fields inside in some addselect in querybuilder?如何在querybuilder中的一些addselect中对字段进行分组?
【发布时间】:2019-07-01 05:09:45
【问题描述】:

我想要的很简单,我想用 querybuilder 将查询的特定字段与别名在 3 个不同级别的关系中分组,更清楚地说,我有 3 个相关表,consulta(医疗咨询),paciente(pacient) ,医生(医生),usuario(用户),所以我在查询中得到的所有内容都是医疗咨询及其患者数据,加上医生数据及其相关用户数据,我想要的是省略那些相关的不相关字段表,我使用 addSelect 来包含关系级别之间的所有数据,但我得到的是所有字段,我想检索“paciente”表的特定字段,以及用户表(这是用户相关医生的),我将展示该计划的方式:

Consulta :{
        // omit the other fields
         {paciente: name and last name}
         {doctor: { usuario: name and last name}}}

$queryBuilder = $em->getRepository('MedicoBundle:Consulta')
               ->createQueryBuilder('e')
               ->leftJoin('e.paciente', 'a')
               ->addSelect('a')
               ->leftJoin('e.doctor', 'o')
               ->addSelect('o')
               ->leftJoin('o.usuario', 'u')
               ->addSelect('u')
               ->where('e.fecha =:fecha')
               ->orderBy('e.id', 'ASC')
               ->setParameter('fecha', $fecha)
               ->getQuery();

它检索所有还不错的字段,但我想检索其中一些字段并省略其他字段,如我在方案中显示的那样

【问题讨论】:

    标签: json symfony orm doctrine


    【解决方案1】:

    只需选择您想要的,而不是选择所有内容:

    $queryBuilder = $em->getRepository('MedicoBundle:Consulta')
                   ->createQueryBuilder('e')
                   ->leftJoin('e.paciente', 'a')
                   ->addSelect('a.name, a.lastname')
                   ->leftJoin('e.doctor', 'o')
                   ->addSelect('o.name, o.lastname')
                   ->leftJoin('o.usuario', 'u')
                   ->where('e.fecha =:fecha')
                   ->orderBy('e.id', 'ASC')
                   ->setParameter('fecha', $fecha)
                   ->getQuery();
    

    虽然我不知道你是怎么称呼你的名字和姓氏属性的,所以要小心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2021-10-11
      相关资源
      最近更新 更多