0

I was using this Stackoverflow as an example:

Which looks like this:

$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria->where($criteria->expr()->gt('prize', 200));

$result = $entityRepository->matching($criteria);

I would like to to use the max instead like this:

$criteria->where($criteria->expr()->max('prize'));

but get:

Attempted to call an undefined method named "max" of class "Doctrine\Common\Collections\ExpressionBuilder".

Any ideas?

Community
  • 1
  • 1
shayster01
  • 214
  • 3
  • 17
  • What are you trying to do? As you see, you cannot use `max` as an expression in the `where` clause. You may have to manually write out the sql using a sub-query: http://stackoverflow.com/questions/1475589/sql-server-how-to-use-an-aggregate-function-like-max-in-a-where-clause – ClickLabs Jan 10 '17 at 01:53

2 Answers2

2

I looked at the API, and it does not have a max expression Comparison.

Here is the Criteria API for reference as well: http://www.doctrine-project.org/api/collections/1.3/class-Doctrine.Common.Collections.Criteria.html

I know querybuilder has a max expression, but this API does not.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
1

It seems that you’re actually looking for the max() method of the Expression builder attached to the query builder Doctrine\ORM\Query\Expr::max().

What you actually are looking for is:

$entityManager->createQueryBuilder()->expr()->max($x);

$entityManager is obviously an instance of your entity manager, which you’ll get through DI or the container.

lxg
  • 12,375
  • 12
  • 51
  • 73