Symfony Querybuilder左联接合并数组

时间:2018-10-11 09:35:28

标签: php sql symfony doctrine query-builder

我目前在使用查询生成器和左联接时遇到问题。我想要实现的是从Article-Entity中选择所有列,并使用带有两个条件的左连接,将字段“追加”到该数组。在代码段中,您将看到没有联接的工作查询以及带有左联接的查询及其结果。

我知道您可以使用“ getScalarResult”,但这也会产生摘要中显示的结果-它为表“ Article”中的所有列加上前缀。我想要的是没有“ a_”前缀的结果。

我知道您可以在select语句中列出表字段,但是我不希望该查询为静态查询,而是希望在表结构 可能更改时动态查询。

// default query
return $this->createQueryBuilder('a')
            ->andWhere('a.publishedAt IS NOT NULL')
            ->andWhere('a.publishedAt <= CURRENT_TIMESTAMP()')
            ->andWhere('a.status = 2')
            ->orderBy('a.publishedAt', 'DESC')
            ->getQuery()
            ->getResult();
// default result
array:5 [
  0 => Article {#723 
    -id: 18
    -title: "Title!"
    -slug: "slug"
    -content: "content"
    -image: "img.jpg"
    -author: BackendUser {#817 ▶}
    -status: 2
    -publishedAt: DateTime @1536546264 {#720 ▶}
    -ratingPositive: 1
    -ratingNegative: 0
    -articleRatings: PersistentCollection {#816 ▶}
    #createdAt: DateTime @1539163998 {#715 ▶}
    #updatedAt: DateTime @1539180102 {#709 ▶}
  }
  ...
]



// Left-Join-Query:
return $this->createQueryBuilder('a')
            ->andWhere('a.publishedAt IS NOT NULL')
            ->andWhere('a.publishedAt <= CURRENT_TIMESTAMP()')
            ->andWhere('a.status = 2')
            ->leftJoin('App\Entity\Article\ArticleRating', 'ar', 'WITH', 'ar.article = a.id AND ar.user = ' . $userId)            
            ->addSelect('ar.rating as userRating')
            ->orderBy('a.publishedAt', 'DESC')
            ->getQuery()
            ->getResult();

// Left Join Result
array:5 [
  0 => array:2 [
    0 => Article {#722 ▼
      -id: 18
      -title: "Title"
      -slug: "Slug"
      -content: "Content"
      -image: "image.jpg"
      -author: BackendUser {#817 ▶}
      -status: 2
      -publishedAt: DateTime @1536546264 {#689 ▶}
      -ratingPositive: 1
      -ratingNegative: 0
      -articleRatings: PersistentCollection {#816 ▶}
      #createdAt: DateTime @1539163998 {#737 ▶}
      #updatedAt: DateTime @1539180102 {#732 ▶}
    }
    "userRating" => true
  ]
  ...
]



// getScalarResult - result
array:5 [▼
  0 => array:12 [▼
    "a_id" => 18
    "a_title" => "Title"
    "a_slug" => "slug"
    "a_content" => "content"
    "a_image" => "image.jpg"
    "a_status" => 2
    "a_publishedAt" => DateTime @1536546264 {#689 ▶}
    "a_ratingPositive" => 1
    "a_ratingNegative" => 0
    "a_createdAt" => DateTime @1539163998 {#737 ▶}
    "a_updatedAt" => DateTime @1539180102 {#732 ▶}
    "userRating" => "1"
  ]
  ...
]

我想要的结果看起来像这样:

array:5 [
    0 => Article {#722 ▼
      -id: 18
      -title: "Title"
      -slug: "Slug"
      -content: "Content"
      -image: "image.jpg"
      -author: BackendUser {#817 ▶}
      -status: 2
      -publishedAt: DateTime @1536546264 {#689 ▶}
      -ratingPositive: 1
      -ratingNegative: 0
      -articleRatings: PersistentCollection {#816 ▶}
      #createdAt: DateTime @1539163998 {#737 ▶}
      #updatedAt: DateTime @1539180102 {#732 ▶}
      -userRating- => true
    }
  ...
]

0 个答案:

没有答案