Question

What is the difference between using ? or ?1 in a DQL query? E.g.

$qb->add('select', 'u')
   ->add('from', 'User u')
   ->add('where', 'u.id = ?1') //<-------
   ->add('orderBy', 'u.name ASC');
   ->setParameter(1, 100);

or

$qb->add('select', 'u')
   ->add('from', 'User u')
   ->add('where', 'u.id = ?') //<-------
   ->add('orderBy', 'u.name ASC');
   ->setParameter(1, 100);
Was it helpful?

Solution

Just using ? would make multiple parameters be indexed in that order, whereas explicitly defining the index with ?1 would allow you to number the parameters in any order.

$qb->add('select', 'u')
   ->add('from', 'User u')
   ->add('where', $qb->expr()->orx(
       $qb->expr()->eq(    'u.id',       '?2'), // using 2 before 1 because I can
       $qb->expr()->like(  'u.nickname', '?1')
   ))
   ->add('orderBy', 'u.name ASC');
   ->setParameter(1, 'bob');  // bound to u.nickname
   ->setParameter(2, 100);    // bound to u.id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top