Symfony 条令-查询生成器-选择leftjoin的少数列

Symfony 条令-查询生成器-选择leftjoin的少数列,symfony,select,doctrine,left-join,Symfony,Select,Doctrine,Left Join,我有两个实体: 注:id、标题、说明、ISPpublic、用户 用户:id、电子邮件、名字、姓氏、密码、salt、角色、标签 当我获取数据库的所有注释时,我只想选择用户的几列(例如,为了不获取用户的密码) 因此,在NoteRepository中,我提出了如下原则请求: $qb = $this->createQueryBuilder('n'); //Get the owner of the knowledge $qb ->leftJoin('n.user', 'owner'

我有两个实体:

  • 注:id、标题、说明、ISPpublic、用户
  • 用户:id、电子邮件、名字、姓氏、密码、salt、角色、标签
当我获取数据库的所有注释时,我只想选择用户的几列(例如,为了不获取用户的密码)

因此,在NoteRepository中,我提出了如下原则请求:

$qb = $this->createQueryBuilder('n');
//Get the owner of the knowledge
$qb
    ->leftJoin('n.user', 'owner')
    ->addSelect('owner.tag as ownerTag, owner.firstname as ownerFirstname, owner.lastname as ownerLastname')
;

return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
(我坚持使用join子句,因为我们可以想象不止一个用户像注释的所有者一样)

我得到以下JSON响应:

[
   {
      "0":{
         "id":6,
         "title":"A1",
         "description":"A1",
         "isPublic":false,
         "ownerTag":"#02a4c022d8",
         "ownerFirstname":"ama",
         "ownerLastname":"ama"
      }
   },
   {
      "1":{
         "id":7,
         "title":"Z1",
         "description":"Z1",
         "isPublic":false,
         "ownerTag":"#00a7bd24g8",
         "ownerFirstname":"z",
         "ownerLastname":"z"
      }
   }
]
[
   {
      "0":{
         "id":6,
         "title":"A1",
         "description":"A1",
         "isPublic":false,
         "owner":{
            "tag":"#02a4c022d8",
            "firstname":"ama",
            "lastname":"ama"
         }
      }
   },
   {
      "1":{
         "id":7,
         "title":"Z1",
         "description":"Z1",
         "isPublic":false,
         "owner":{
            "tag":"#00a7bd24g8",
            "firstname":"z",
            "lastname":"z"
         }
      }
   }
]
但我希望得到以下回应:

[
   {
      "0":{
         "id":6,
         "title":"A1",
         "description":"A1",
         "isPublic":false,
         "ownerTag":"#02a4c022d8",
         "ownerFirstname":"ama",
         "ownerLastname":"ama"
      }
   },
   {
      "1":{
         "id":7,
         "title":"Z1",
         "description":"Z1",
         "isPublic":false,
         "ownerTag":"#00a7bd24g8",
         "ownerFirstname":"z",
         "ownerLastname":"z"
      }
   }
]
[
   {
      "0":{
         "id":6,
         "title":"A1",
         "description":"A1",
         "isPublic":false,
         "owner":{
            "tag":"#02a4c022d8",
            "firstname":"ama",
            "lastname":"ama"
         }
      }
   },
   {
      "1":{
         "id":7,
         "title":"Z1",
         "description":"Z1",
         "isPublic":false,
         "owner":{
            "tag":"#00a7bd24g8",
            "firstname":"z",
            "lastname":"z"
         }
      }
   }
]
我不知道怎么弄到它。谢谢你帮助我;)

我找到了这个

我不确定你能不能直接用你的queryBuilder

但是你可以在你的数组上做一些PHP工作:)

我发现了这个

我不确定你能不能直接用你的queryBuilder


但是你可以在你的数组上做一些PHP工作:)

好的,我找到了一个解决方案。 我需要像这样使用部分条款:

$qb = $this->createQueryBuilder('n');
//Get the owner of the knowledge
$qb
    ->leftJoin('n.user', 'owner')
    ->addSelect('PARTIAL owner.{id,tag,firstname,lastname}')
;

return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);

好吧,我找到了解决办法。 我需要像这样使用部分条款:

$qb = $this->createQueryBuilder('n');
//Get the owner of the knowledge
$qb
    ->leftJoin('n.user', 'owner')
    ->addSelect('PARTIAL owner.{id,tag,firstname,lastname}')
;

return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);

如果您执行添加选择('owner.tag,owner.firstname,owner.lastname')我不会做的事情,但是使用'tag'而不是'owner tag'等…如果您执行添加选择('owner.tag,owner.firstname,owner.lastname')会发生什么?我不会做的事情,但是使用'tag'而不是'owner tag等。。。