2017-09-18 19 views
1

エンティティの関連性があり、エンティティを取得するためにdqlクエリを定義する必要があります。関連エンティティを結合するDQL

MAIN ENTITY

class proyectosSubsecciones 
{ 
    ... 

    /** 
    * @ORM\Id 
    * @ORM\ManyToOne(targetEntity="app\SubseccionesBundle\Entity\Subsecciones") 
    * @ORM\JoinColumn(name="id_subseccion", referencedColumnName="id") 
    */ 
    private $subseccion; 

    /** 
    * @ORM\Id 
    * @ORM\ManyToOne(targetEntity="app\ProyectosBundle\Entity\Proyectos") 
    * @ORM\JoinColumn(name="id_proyecto", referencedColumnName="id") 
    */ 
    private $proyecto; 
    .... 

関連のエンティティ私は種類の異なった実体取得する必要が

class subsecciones 
{ 
    ... 

    /** 
    * @ORM\ManyToOne(targetEntity="app\SeccionesBundle\Entity\Secciones") 
    * @ORM\JoinColumn(name="id_seccion", referencedColumnName="id",nullable=false) 
    */ 
    private $seccion; 
    ... 

"アプリを\ SeccionesBundleエンティティ\ Secciones \" それぞれの「アプリ\からProyectosBundle \ Entity \ Proyectos "

のような質問をしています

$consulta=$em->createQuery(' 
      SELECT DISTINCT sc 
      FROM ProyectosSubseccionesBundle:ProyectosSubsecciones p 
      JOIN p.subseccion s WITH s.id=p.subseccion 
      JOIN s.seccion sc WITH sc.id=s.seccion 
      WHERE p.proyecto= :id 
     '); 
$consulta->setParameter('id', $id_proyecto); 
$subsecciones=$consulta->getResult(); 
私はというエラーを取得:

しかし、私は唯一のsc.Anyのアイデアからのデータを必要とする「とは、少なくとも1つのルートエンティティの別名を選択せず​​に識別変数を通してエンティティを選択することはできませんか」? ? ProyectosSubseccionesRepository

+0

のような基準を結ぶ上で別のフィルタを追加したい場合は、「SC」からのデータが必要な場合はProyectos&Secciones –

+0

の定義を共有してください - 「DISTINCT SCを選択」を書きます。なぜそれは "SELECT DISTINCT ss"ですか? "ss"とは何ですか? –

答えて

1

使用クエリビルダー:あなたの問題のために

return $this->createQueryBuilder('p') 
    ->join('p.subseccion', 's', Join::WITH, 's = p.subseccion') 
    ->join('s.seccion', 'sc', Join::WITH, 'sc = s.seccion') 
    ->where('p.proyecto = :id') 
    ->setParameter('id', $id) 
    ->getQuery() 
    ->execute() 
0

私はあなたのエンティティ間のbidirectionalの関係を定義していると仮定します。上記のクエリはSeccionesエンティティを選択し、Seccionesエンティティで定義されたプロパティ$subseccionesを使用してSubseccionesエンティティに参加します

SELECT DISTINCT s 
FROM Secciones s 
JOIN s.subsecciones ss 
JOIN ss.proyectosSubsecciones pss 
JOIN pss.proyecto 
WHERE p.id = :id 

として

bidirectional定義上考慮

Entity      RelationType ReferenceEntity   Reference 
========================================================================================== 
ProyectosSubsecciones  ManyToOne  Subsecciones   $subseccion 
ProyectosSubsecciones  ManyToOne  Proyectos    $proyecto 
Proyectos     OneToMany  ProyectosSubsecciones $proyectosSubsecciones 
Subsecciones    OneToMany  ProyectosSubsecciones $proyectosSubsecciones 
Subsecciones    ManyToOne  Secciones    $seccion 
Secciones     OneToMany  Subsecciones   $subsecciones 

ようにあなたは、あなたのDQLを書くことができます。

クエリはSubseccionesエンティティで定義されたプロパティ$proyectosSubseccionesを使用してProyectosSubseccionesSubseccionesを結合します。

最後に、ProyectosSubseccionesエンティティで定義された$proyectoプロパティを使用してProyectosエンティティとProyectosSubseccionesし、最後にそれがあなたのWHERE句をによるフィルタを適用します。 DQLでは、接合部が、あなたがエンティティまたは間で定義された関係マッピングが存在しない場合に使用されOneToMany/ManyToOne or ManyToManyWITHとして定義するプロパティによってカバーされます、ので、あなたの事業体に参加するWITH句を使用する必要はありません

注意あなたはON(a.id = b.some_id AND/WITH some = some)

関連する問題