ActiveDataProvider count() with having()

I am trying to take advantage of all the cool features of ActiveDataProvider but hit a road block where this is a SUM + HAVING statement in my query





	public function search($params = [])

	{

		$query = $this->buildSearch($params);

		$countQuery = clone $query;

    	        $pages = new Pagination(['totalCount' => $countQuery->count()]);

		$models = $query->offset(\yii\helpers\ArrayHelper::getValue($params, 'page', $pages->offset))

                ->limit($pages->limit)

                ->all();

		return new ActiveDataProvider([

			'query'      => $query,

			'pagination' => $pages,

			'models'     => $models,

		]);

	}


public function buildSearch($params = [])

	{


.

.

.

		$query->sum('oi.price');

		$query->addSelect('oi.name');

		$query->leftJoin('order o', 'o.customer_id = cu.id');

		$query->leftJoin('order_item oi', 'oi.order_id = o.id');

		$query->having([ '>', 'oi.price', $minSpend ]);

.

.

.

                return $query;

}



The error I get is

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘oi.price’ in ‘having clause’

The SQL being executed was: SELECT COUNT(*) FROM customer cu LEFT JOIN order o ON o.customer_id = cu.id LEFT JOIN order_item oi ON oi.order_id = o.id WHERE oi.name LIKE ‘%10%’ HAVING oi.price > ‘10’

Any suggestions?

Changing

$query->sum(‘oi.price’);

to

$query->addSelect(‘SUM(oi.price) AS spend’);

fixed the issue.

Incorrect usage of sum method!