[PR #648] [CLOSED] Add a AddParameters method in the QueryBuilder #8507

Closed
opened 2026-01-22 16:00:13 +01:00 by admin · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/doctrine/orm/pull/648
Author: @Taluu
Created: 4/13/2013
Status: Closed

Base: masterHead: query-builder-add-parameters


📝 Commits (2)

  • 7d702f6 Add a AddParameters method in the QueryBuilder
  • c5ee75a Added another assertion for the addParameters test

📊 Changes

2 files changed (+56 additions, -0 deletions)

View changed files

📝 lib/Doctrine/ORM/QueryBuilder.php (+32 -0)
📝 tests/Doctrine/Tests/ORM/QueryBuilderTest.php (+24 -0)

📄 Description

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 one 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('criteriaB', $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.

edit : If I tried to use either a ArrayCollection or an array in the parameters, it's because I tried to keep in mind the BC breeaks for 2.2 and below branches, and if I am converting the Parameter into a simple array, it is to ease the use for setParameter.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/doctrine/orm/pull/648 **Author:** [@Taluu](https://github.com/Taluu) **Created:** 4/13/2013 **Status:** ❌ Closed **Base:** `master` ← **Head:** `query-builder-add-parameters` --- ### 📝 Commits (2) - [`7d702f6`](https://github.com/doctrine/orm/commit/7d702f638b813e407eba5af8b92c258c4093e9a4) Add a AddParameters method in the QueryBuilder - [`c5ee75a`](https://github.com/doctrine/orm/commit/c5ee75a52d88e69590b0624d1f9d24d044f757b8) Added another assertion for the addParameters test ### 📊 Changes **2 files changed** (+56 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `lib/Doctrine/ORM/QueryBuilder.php` (+32 -0) 📝 `tests/Doctrine/Tests/ORM/QueryBuilderTest.php` (+24 -0) </details> ### 📄 Description 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 one 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('criteriaB', $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. **edit :** If I tried to use either a `ArrayCollection` or an `array` in the parameters, it's because I tried to keep in mind the BC breeaks for 2.2 and below branches, and if I am converting the `Parameter` into a simple array, it is to ease the use for `setParameter`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
admin added the pull-request label 2026-01-22 16:00:13 +01:00
admin closed this issue 2026-01-22 16:00:14 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#8507