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 4 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

I also have this issue (merging logic coming from a client application with the logic on the server). Would be nice if this could be fixed.

andreas-aeschlimann avatar Jan 06 '24 00:01 andreas-aeschlimann

any update on this?

karrakoliko avatar May 02 '24 15:05 karrakoliko

Yes, see https://github.com/doctrine/orm/pull/10475

greg0ire avatar May 02 '24 19:05 greg0ire

so, the only option is to create fork that includes mentioned PR commit? isn't that considered as bug by doctrine devs at all (i see no bug label on the issue)?

karrakoliko avatar May 06 '24 08:05 karrakoliko

I'll add the bug label. There, happy?

greg0ire avatar May 06 '24 09:05 greg0ire