orm icon indicating copy to clipboard operation
orm copied to clipboard

"Too many parameters: ..." when adding multiple criteria having clauses on the same property

Open alexpozzi opened this issue 3 years ago • 5 comments

Hello, First of all, thanks for all the work you contributors put in this library. I'm trying to simplify my repository queries externalizing some logic to criteria. Everything works pretty well and the repository is more readable but, on complex queries, where I have to add multiple criteria that are adding clauses on the same property, Doctrine is returning me an error saying that I have the wrong number of parameters defined. Debugging the code I saw that, separate criteria (with a clause on the same property) are setting parameters with the same name and, as far as I saw, the parameter names univocity is ensured only within the same criteria.

The below code it's just an example of a possible scenario that leads to the Too many parameters: ... error (I know that this case can be dealt differently but I have other cases that are trickier).

// Repository
function getArticles():
{
    $qb = $this->createQueryBuilder('article');
    $qb->addCriteria(self::createDateToCriteria($dateTo));
    $qb->addCriteria(self::createDateFromCriteria($dateFrom));

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

static function createDateToCriteria($date)
{
    $criteria = Criteria::create();
    $criteria->andWhere(Criteria::expr()->lte( 'publishedAt', $date));

   return $criteria;
}

static function createDateFromCriteria($date)
{
    $criteria = Criteria::create();
    $criteria->andWhere(Criteria::expr()->gte( 'publishedAt', $date));

   return $criteria;
}

Is this a known issue? Is there a solution to be able to set parameter names on criteria? In case you are interested, I'm available to contribute to try to fix the issue.

alexpozzi avatar May 20 '21 08:05 alexpozzi