SensacionRC
SensacionRC

Reputation: 615

DQL joining related entities

Hi have some entities relateds and need to define a dql query to obtain an entity.

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;
    ....

RELATED ENTITY

class subsecciones
{
    ...

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

I need to obtain the distinct entities of type "app\SeccionesBundle\Entity\Secciones" from each "app\ProyectosBundle\Entity\Proyectos"

I´m trying a query like:

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

I get an error that says:

"Cannot select entity through identification variables without choosing at least one root entity alias"

But I only need the data from sc.Any idea??

Upvotes: 2

Views: 907

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

For your problem i assume you have defined bidirectional relationship among your entities.

like

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

Considering above bidirectional definitions you can write your DQL as

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

The above query will select Secciones entity and join with Subsecciones entity using property $subsecciones defined in Secciones entity.

Then query will join Subsecciones with ProyectosSubsecciones using property $proyectosSubsecciones defined in Subsecciones entity.

Finally it will ProyectosSubsecciones with Proyectos entity using $proyecto property defined in ProyectosSubsecciones entity and lastly it will apply a filter according your WHERE clause.

Note there is no need to use WITH clause to join your entities because, In DQL, joining part will be covered by properties which you define as OneToMany/ManyToOne or ManyToMany WITH is used for if there is no relation mapping defined between entities or if you want to add another filter on joining criteria like ON(a.id = b.some_id AND/WITH some = some)

Upvotes: 0

genesst
genesst

Reputation: 1431

Use query builder in ProyectosSubseccionesRepository:

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

Upvotes: 2

Related Questions