(doctrine2查询构建器)如何创建嵌套数组或结构?

时间:2016-05-13 09:08:51

标签: php symfony orm doctrine-orm

我使用doctrine2中的查询构建器创建了一个查询:

   $result = $this->createQueryBuilder('a')
                 ->select('a.id, a.title, a.createdAt, t.name as tags, ac.content, ac.contentType, u.username')
                 ->join('AppBundle\Entity\ArticleContent', 'ac', 'WITH', 'a.id = ac.article_id')
                 ->join('AppBundle\Entity\User', 'u', 'WITH', 'a.user_id = u.id')
                 ->join('AppBundle\Entity\Tag', 't', 'WITH', 't.name LIKE :name')
                 ->setMaxResults($maxResults)
                 ->setParameter('name', $name)
                 ->getQuery()
                 ->getArrayResult();

我创建了一个如下所示的数组:

    array (size=4)
  0 => 
    array (size=7)
      'id' => int 1
      'title' => string 'PHP' (length=3)
      'createdAt' => string '2016-05-12 11:36:13' (length=19)
      'tags' => string 'TDD' (length=3)
      'content' => string 'Lorem Ipsum' (length=11)
      'contentType' => string 'Code' (length=4)
      'username' => string 'Test user1' (length=10)
  1 => 
    array (size=7)
      'id' => int 2
      'title' => string 'JAVA' (length=4)
      'createdAt' => string '2016-05-12 11:36:13' (length=19)
      'tags' => string 'OOP' (length=3)
      'content' => string 'Lorem Ipsum' (length=11)
      'contentType' => string 'Media' (length=4)
      'username' => string 'Test user' (length=9)
  2 => 
    array (size=7)
      'id' => int 1
      'title' => string 'PHP' (length=3)
      'createdAt' => string '2016-05-12 11:36:13' (length=19)
      'tags' => string 'TDD' (length=3)
      'content' => string 'Lorem Ipsum' (length=11)
      'contentType' => string 'Code' (length=4)
      'username' => string 'Test user1' (length=10)
  3 => 
    array (size=7)
      'id' => int 2
      'title' => string 'JAVA' (length=4)
      'createdAt' => string '2016-05-12 11:36:13' (length=19)
      'tags' => string 'OOP' (length=3)
      'content' => string 'Lorem Ipsum' (length=11)
      'contentType' => string 'Media' (length=4)
      'username' => string 'Test user' (length=9)

但我想创建一个如下所示的数组:

array (size=5)
  0 => 
    array (size=7)
      'id' => int 1
      'title' => string 'PHP' (length=3)
      'createdAt' => string '2016-05-12 11:36:13' (length=19)
      'tags' => string 'TDD' (length=3)
      'contents' => array (size=2)
                         array (size=2)
                            'content' => string 'Lorem Ipsum' (length=11)
                            'contentType' => string 'Code' (length=4)
                         array (size=2)
                            'content' => string 'Lorem Ipsum' (length=11)
                            'contentType' => string 'Media' (length=4)
      'username' => string 'Test user1' (length=10)

如何使用doctrine2创建它?有没有办法做到这一点。

0 个答案:

没有答案