Question

I have an issue with DQL in Doctrine 2.

Subqueries seem to be unavailable in DQL, so I don't know how to transform :

SELECT DISTINCT a.ID_DOMAINE, L_DOMAINE, b.ID_SS_DOMAINE, L_SS_DOMAINE, c.ID_COMPETENCE,     L_COMPETENCE
    FROM ((qfq_prod.REF_DOMAINE a inner join qfq_prod.REF_SS_DOMAINE b on a.id_domaine = b.id_domaine)
inner join qfq_prod.REF_COMPETENCE c on b.id_ss_domaine = c.id_ss_domaine)
inner join qfq_prod.REF_PERS_COMP d on c.id_competence = d.id_competence

into a DQL expression.

I tried it and got

"Error: Class '(' is not defined."

I saw that we can use Query Builder to do this as well.

Being new with Doctrine 2, can someone explain to me how I can do this please ?

My DQL is currently :

$query = $this->getEntityManager()->createQuery ( "SELECT DISTINCT a.ID_DOMAINE, L_DOMAINE, b.ID_SS_DOMAINE, L_SS_DOMAINE, c.ID_COMPETENCE, L_COMPETENCE
FROM ((BdDoctrine\Entity\Domaine a inner join BdDoctrine\Entity\SsDomaine b on a.id_domaine = b.id_domaine)
inner join BdDoctrine\Entity\Competence c on b.id_ss_domaine = c.id_ss_domaine)
inner join BdDoctrine\Entity\LienPersComp d on c.id_competence = d.id_competence" );

$res = $query->getResult ();
Was it helpful?

Solution

Subqueries seem to be unavailable in DQL, so I don't know how to transform :

Actually, they are. Your code (no offence) is hardly readable so I will give you an example:

//controller
$repo = $this->getDoctrine()->getRepository("Your:Bundle:Category") ;
$results = $repo->findAllForSomePage() ;

// CategoryRepository.php
public function findAllForSomePage()
{
    return $this->createQueryBuilder("o")
        ->innerJoin("o.products", "p", "WITH", "p.price>:price")->addSelect("p")
            ->setParameter("price", 50)
        ->where("o.id IN (SELECT s1.id FROM Your:Bundle:Something s1 WHERE s1.col1=5)")
        ->getQuery()->getResult() ;
}

Here is presumed you have Category hasMany Products relation and that you defined CategoryRepository file. You should never create queries in controller.

This example will fetch Categories only if they have Products with price bigger than 50, AND the ID of categories are those fetched by fictional subquery. This 100% works.

You should apply the same logic on your requirement.

Also, you should not use ON statement when using joins, that is handled by doctrine.

OTHER TIPS

If you have the relationships properly defined in your entities, then you can make your joins on those relationships. And as Zeljko mentioned, you don't need to specify the ON condition, as the entities should already know how they are related. You are joining entities not tables. (That's under the hood.)

I don't know what your entities look like, so I made a guess at the relationship names below, but it should give you the idea.

$dql = 
<<<DQL    
SELECT 
    DISTINCT a.ID_DOMAINE, b.L_DOMAINE, b.ID_SS_DOMAINE, b.L_SS_DOMAINE, c.ID_COMPETENCE, c.L_COMPETENCE
FROM 
    BdDoctrine\Entity\Domaine a 
    JOIN a.ss_domaine b
    JOIN b.competence c
    JOIN c.lien_pers_comp d
DQL;

$query = $this->getEntityManager()->createQuery($dql);

$res = $query->getResult();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top