DDC-2400: [GH-648] Add a AddParameters method in the QueryBuilder #3012

Closed
opened 2026-01-22 14:09:48 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 13, 2013).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user @doctrinebot:

This issue is created automatically through a Github pull request on behalf of Taluu:

Url: https://github.com/doctrine/doctrine2/pull/648

Message:

Hi,

On doctrine 2.2, when we were calling the method setParameters to set a bunch of parameters on the QueryBuilder, each parameters were added or modified if they were already existing.

That is not the case since doctrine 2.3 : each calls to setParameters will wipe out the other already setted parameters. I think it is logic, but still is a huge BC break, which is not really mentionned in the UPGRADE file (only the once regarding the use of an ArrayCollection is mentionned).

With this PR, I added a addParameters which allows to add several parameters in several calls. Here is a use case :

<?php
// ... A repository
class UserRepository extends EntityRepository
{
     public function getUserWithSomething($criteriaA, $criteriaB, DateTime $since = null)
     {
         $qb = $this->createQueryBuilder('u');
         $qb->where($qb->expr()->andX($qb->expr()->eq('u.criteriaA', ':criteriaA'),
                                                       $qb->expr()->eq('u.criteriaB', ':criteriaB')));

         // wanna search for objects since a specific date
         if (null !== $since) {
             $qb = $this->addSince($qb, $since);
         }

         $qb->addParameters(new ArrayCollection(['criteriaA' => $criteriaA, 
                                                                       'criteriaB' => $criteriaB]));

         return $qb->getQuery()->execute();
     }

     public function addSince(QueryBuilder $qb, DateTime $since)
     {
         $qb = $qb->andWhere($qb->expr()->gte('u.date', ':since'));

         return $qb->setParameter('since', $since);
     }
}

So, as I was saying, until 2.2, you could use setParameters in the main method (getUserWithSomething) which was calling (or not) the submethod addSince, which could set parameters before calling the setParameters method. Now, you can't do that anymore : either you need to make several calls to setParameter :

<?php
// ....
     $qb = $qb->setParameter('criteriaA', $criteriaA)
                   ->setParameter('criteraB', $criteriaB);
//...

Either, I guess, you need to first retrieve all the parameters and then add them by hand :

<?php
// ....
     $parameters = $qb->getParameters();
     $parameters->add(new Parameter('criteriaA', $criteriaA);
     $parameters->add(new Parameter('criteriaB', $criteriaB);

     $qb = $qb->setParameters($parameters);
//...

So that's why I propose this new method in the QueryBuilder. if I'm wrong anywhere, or need more information please feel free to comment. :)

Cheers.

Originally created by @doctrinebot on GitHub (Apr 13, 2013). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user @doctrinebot: This issue is created automatically through a Github pull request on behalf of Taluu: Url: https://github.com/doctrine/doctrine2/pull/648 Message: Hi, On doctrine 2.2, when we were calling the method `setParameters` to set a bunch of parameters on the QueryBuilder, each parameters were added or modified if they were already existing. That is not the case since doctrine 2.3 : each calls to `setParameters` will wipe out the other already setted parameters. I think it is logic, but still is a huge BC break, which is not really mentionned in the UPGRADE file (only the once regarding the use of an ArrayCollection is mentionned). With this PR, I added a `addParameters` which allows to add several parameters in several calls. Here is a use case : ``` php <?php // ... A repository class UserRepository extends EntityRepository { public function getUserWithSomething($criteriaA, $criteriaB, DateTime $since = null) { $qb = $this->createQueryBuilder('u'); $qb->where($qb->expr()->andX($qb->expr()->eq('u.criteriaA', ':criteriaA'), $qb->expr()->eq('u.criteriaB', ':criteriaB'))); // wanna search for objects since a specific date if (null !== $since) { $qb = $this->addSince($qb, $since); } $qb->addParameters(new ArrayCollection(['criteriaA' => $criteriaA, 'criteriaB' => $criteriaB])); return $qb->getQuery()->execute(); } public function addSince(QueryBuilder $qb, DateTime $since) { $qb = $qb->andWhere($qb->expr()->gte('u.date', ':since')); return $qb->setParameter('since', $since); } } ``` So, as I was saying, until 2.2, you could use `setParameters` in the main method (`getUserWithSomething`) which was calling (or not) the submethod `addSince`, which could set parameters before calling the `setParameters` method. Now, you can't do that anymore : either you need to make several calls to `setParameter` : ``` php <?php // .... $qb = $qb->setParameter('criteriaA', $criteriaA) ->setParameter('criteraB', $criteriaB); //... ``` Either, I guess, you need to first retrieve all the parameters and then add them by hand : ``` php <?php // .... $parameters = $qb->getParameters(); $parameters->add(new Parameter('criteriaA', $criteriaA); $parameters->add(new Parameter('criteriaB', $criteriaB); $qb = $qb->setParameters($parameters); //... ``` So that's why I propose this new method in the QueryBuilder. if I'm wrong anywhere, or need more information please feel free to comment. :) Cheers.
admin added the Bug label 2026-01-22 14:09:48 +01:00
admin closed this issue 2026-01-22 14:09:49 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 14, 2013):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-648] was closed:
https://github.com/doctrine/doctrine2/pull/648

@doctrinebot commented on GitHub (Apr 14, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-648] was closed: https://github.com/doctrine/doctrine2/pull/648
Author
Owner

@doctrinebot commented on GitHub (Apr 14, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Apr 14, 2013): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Aug 4, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-648] was assigned:
https://github.com/doctrine/dbal/pull/648

@doctrinebot commented on GitHub (Aug 4, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-648] was assigned: https://github.com/doctrine/dbal/pull/648
Author
Owner

@doctrinebot commented on GitHub (Aug 4, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-648] was unassigned:
https://github.com/doctrine/dbal/pull/648

@doctrinebot commented on GitHub (Aug 4, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-648] was unassigned: https://github.com/doctrine/dbal/pull/648
Author
Owner

@doctrinebot commented on GitHub (Sep 11, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-648] was closed:
https://github.com/doctrine/dbal/pull/648

@doctrinebot commented on GitHub (Sep 11, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-648] was closed: https://github.com/doctrine/dbal/pull/648
Author
Owner

@doctrinebot commented on GitHub (Sep 11, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-648] was assigned:
https://github.com/doctrine/dbal/pull/648

@doctrinebot commented on GitHub (Sep 11, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-648] was assigned: https://github.com/doctrine/dbal/pull/648
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3012