22

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

但是,我无法构建正确的查询构建器语句,因为我不知道如何引用superCategoryfrom my ,因为我的类别 IDcategory中没有字段。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);
4

2 回答 2

43

知道了 :

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

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

于 2012-04-08T07:04:31.240 回答
3

就像是:

$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();
于 2012-04-06T08:22:59.157 回答