Gabriel Muñumel
Gabriel Muñumel

Reputation: 1876

Two joins and a where - Doctrine2

I have the following query in Doctrine2.

$dql->select('um', 'u', 'r')
                    ->from('AcmeComBundle:UserMenu', 'um')
                    ->join('um.user', 'u')
                    ->join('u.role', 'r')
                    ->where('u.ced = '.$ced);

Always got problems referring to:

[Semantical Error] line 0, col 116 near 'XXXXX': Error: 'XXXXX' is not defined.

The XXXXX is from $ced.

Any idea?

Upvotes: 1

Views: 158

Answers (3)

Gabriel Muñumel
Gabriel Muñumel

Reputation: 1876

I'm really stupid.

The solution is:

$dql->select('um', 'u', 'r')
                ->from('AcmeComBundle:UserMenu', 'um')
                ->join('um.user', 'u')
                ->join('u.role', 'r')
                ->where("u.ced = '".$ced."'");

Thanks guys for your answers.

Upvotes: 0

Lusitanian
Lusitanian

Reputation: 11122

You should be using bound params, not directly putting the "$ced" into the where statement. Change your where statement to this:

 $dql->where('u.ced = :ced')->setParameter('ced', $ced);

Otherwise, not only will the generated {D/S}QL become invalid, but you are vulnerable to injection attacks.

Upvotes: 2

manix
manix

Reputation: 14747

Hmm if the error persists you can try by this way as documented in doctrine 2.1 docs:

$em = $this->getEntityManager;
$qb = $em->createQueryBuilder;

$qb->select(array('um', 'u', 'r'))
   ->from('Sdz\BlogBundle\Entity\Article', 'a')
   ->leftJoin('um.user', 'u')
   ->leftJoin('u.role', 'r')
   ->where('u.ced = '.$ced);

$query = $qb->getQuery();
$results = $query->getResult();

return $results;

Upvotes: 0

Related Questions