php 查询构建器加入多对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10038581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:11:45  来源:igfitidea点击:

Query builder join on ManyToMany relationship

phpsymfonydoctrine-orm

提问by fkoessler

I am trying to use query builder to select all the categories that belong to a certain superCategory(categoryand superCategoryhave a many to many relationship).

我正在尝试使用查询构建器来选择属于某个superCategorycategory并且superCategory具有多对多关系)的所有类别。

However, I am unable to build a correct query builder sentence because I don't know how to reference to the superCategoryfrom my categoryas there is no superCategoryfield inside my category ID.

但是,我无法构建正确的查询构建器语句,因为我不知道如何引用superCategoryfrom my,category因为superCategory我的类别 ID 中没有字段。

The objects in the database look like :

数据库中的对象如下所示:

Category:
  id
  name

SuperCategory
  id
  name

categories_superCategories
  id
  category_id
  superCategory_id

Here are the definition of my objects (yml files):

以下是我的对象(yml 文件)的定义:

YOP\YourOwnPoetBundle\Entity\TraitCategory:
  type: entity
  repositoryClass: YOP\YourOwnPoetBundle\Repository\TraitCategoryRepository
  table: null
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    name:
      type: string
      length: '255'
  lifecycleCallbacks: {  }
  manyToMany:
    superCategories:
      targetEntity: SuperCategory
      joinTable:
        name: traitCategories_superCategories
        joinColumns:
          traitCategory_id:
            referencedColumnName: id
        inverseJoinColumns:
          superCategory_id:
            referencedColumnName: id

and

YOP\YourOwnPoetBundle\Entity\SuperCategory:
  type: entity
  repositoryClass: YOP\YourOwnPoetBundle\Repository\SuperCategoryRepository
  table: null
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    name:
      type: string
      length: '255'
  lifecycleCallbacks: {  }
  manyToMany:
    msgCategories:
      targetEntity: MsgCategory
      mappedBy: superCategories
    traitCategories:
      targetEntity: TraitCategory
      mappedBy: superCategories

How would I build a query builder sentence to get the categories that belong to a certain superCategory?

我将如何构建一个查询构建器句子来获取属于某个类别的类别superCategory

The query inside my CategoryRepository:

我的内部查询CategoryRepository

$this->createQueryBuilder('c')
            ->innerJoin( ?????? )
            ->setParameter('superCategoryName', $superCategoryName);

回答by fkoessler

Got it :

知道了 :

public function findBySuperCategoryName($superCategoryName)
{
    return $this->createQueryBuilder('c')
            ->innerJoin('c.superCategories', 's', 'WITH', 's.name = :superCategoryName')
            ->setParameter('superCategoryName', $superCategoryName);
}

The problem was that I had to ask for c.superCategories and not c.superCategory !

问题是我必须要求 c.superCategories 而不是 c.superCategory !

回答by Askent

Something like:

就像是:

$this->createQueryBuilder()
        ->select('s')
        ->from('SuperCategory', 's')
        ->innerJoin('s.Category c ON c.category_id = s.superCategory_id')
        ->where('s.name = :superCategoryName')
        ->setParameter('superCategoryName', $superCategoryName)
        ->getQuery()
        ->getResult();