Подтвердить что ты не робот

Построитель запросов вступает в отношения ManyToMany

Я пытаюсь использовать построитель запросов, чтобы выбрать все категории, которые относятся к определенному superCategory (category и superCategory имеют отношение многие ко многим).

Однако я не могу создать правильное предложение построителя запросов, потому что я не знаю, как ссылаться на superCategory из моего category, поскольку внутри идентификатора категории нет поля superCategory.

Объекты в базе данных выглядят так:

Category:
  id
  name

SuperCategory
  id
  name

categories_superCategories
  id
  category_id
  superCategory_id

Вот определение моих объектов (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

и

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

Как мне построить предложение построителя запросов, чтобы получить категории, принадлежащие определенной superCategory?

Запрос внутри моего CategoryRepository:

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

Ответ 1

Получил это:

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

Проблема заключалась в том, что мне пришлось просить c.superCategories, а не c.superCategory!

Ответ 2

Что-то вроде:

$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();