0
votes

I want to add a join to my query in if condition but it returned an error. how we can do it in doctrine? is there any thing like: $qb->andWhere('p.$gender = :g')->setParameter('g', $gender); for join in doctrine symfony??

   $qb->select("p")->from("PfmSanadBundle:Person",  "p");

    if ((isset($province) && trim($province) !== '') && (isset($City) && trim($City) !== '')){
        $qb->join("p" ,"students", "ps")
            ->join("ps" ,"organization", "po")
            ->join("po" ,"cityProvince", "pc")
            ->join("pc" ,"province", "pp");
    }
   if ((isset($province) && trim($province) !== '') && !isset($City)) {
        $qb->join("p.students", "ps")
            ->join("ps.organization", "po");
    }
    $res = $qb->getQuery()->getArrayResult();

error (postman):

"error": { "code": 500, "message": "Internal Server Error", "exception": [ { "message": "[Semantical Error] line 0, col 49 near 'p students INNER': Error: Class 'p' is not defined.", "class": "Doctrine\ORM\Query\QueryException", "trace": [ {

1

1 Answers

0
votes

Join method usage is following

// Example - $qb->join('u.Group', 'g', Expr\Join::WITH, $qb->expr()->eq('u.status_id', '?1'))
// Example - $qb->join('u.Group', 'g', 'WITH', 'u.status = ?1')
// Example - $qb->join('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null);

Thus following lines

$qb->join("p" ,"students", "ps")
    ->join("ps" ,"organization", "po")
    ->join("po" ,"cityProvince", "pc")
    ->join("pc" ,"province", "pp");

change to

$qb->join("p.students", "ps")
    ->join("ps.organization", "po")
    ->join("po.cityProvince", "pc")
    ->join("pc.province", "pp");