Home > database >  Need help for performing join Query with QueryBuilder
Need help for performing join Query with QueryBuilder

Time:11-01

I got a working SQL query : select p.name, p.id from acv_project p join acv_product prod on prod.project_id = p.id where prod.weight <> 0 and p.green_user_id = 18

If i pass it into a `

$stmt = $em->getConnection()->prepare($rawSql);
        $stmt->execute([]);

        $projects = $stmt->fetchAll();

It works but i'd like to pass it by adding the "green_user_id" as a parameter and not always 18.

When i try with this code : `

$sql2 = "select p from ArtoAcvBundle:Project p join prod ArtoAcvBundle:Product on prod.project_id = p.id where prod.weight <> 0 and p.green_user_id =:userId";
        $query2 = $em->createQuery($sql2)->setParameters(
                array('userId' => $userId));
        
        $projects = $query2->getResult();

I get [Semantical Error] line 0, col 48 near 'ArtoAcvBundle:Product': Error: Identification Variable prod used in join path expression but was not defined before.

And with QueryBuilder, i tried lots of thing but fails to understand how to write it.

Here are some links to my 2 Doctrine entities :

Entity Product

Entity Project

Thanks for help !

CodePudding user response:

Proof with:

$sql2 = "select p from ArtoAcvBundle:Project p join ArtoAcvBundle:Product prod where prod.weight <> 0 and p.green_user_id =:userId";
    

CodePudding user response:

Yep, great thanks for having found this solution. I continued to search and find there existed a bindValue() method in Doctrine.

So i passed my parameter with the raw SQL modified and it works

  • Related