We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Subquery column alias in having

I'm getting an error when trying to use a column alias in the having clause. Am I doing something wrong or this is not supported?

$query = $this->modelsManager->createBuilder()
    ->addFrom('Iwerp\Models\Products', 'Products')
    ->columns([
        'productId' => 'Products.id',
        'requiredQuantity' => 'SUM(Products.quantity)',
    ])
    ->groupBy('Products.id')
    ->having('requiredQuantity > 0');

$paginator = new PaginatorQueryBuilder([
    'builder' => $query,
    'limit' => $limit,
    'page' => (int) (floor($start / $length) + 1),
]);
Column 'requiredQuantity' doesn't belong to any of the selected models (1), when preparing:
[....]
#0 [internal function]: Phalcon\Mvc\Model\Query->_getQualified(Array)
#1 [internal function]: Phalcon\Mvc\Model\Query->_getExpression(Array, true)
#2 [internal function]: Phalcon\Mvc\Model\Query->_getExpression(Array)
#3 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect()
#4 [internal function]: Phalcon\Mvc\Model\Query->parse()
#5 [internal function]: Phalcon\Mvc\Model\Query->getSql()

This is a simple example, but in my case I have a subquery instead of SUM(Products.quantity) and I would like to not repeat it in the having.



8.4k
edited Dec '20

there is a bug similar to this in phalcon < 4 and fixed in 4

https://github.com/phalcon/cphalcon/pull/13637

there is a bug similar to this in phalcon < 4 and fixed in 4

https://github.com/phalcon/cphalcon/pull/13637

I don't think it's fixed. I'm using phalcon 4.1.0 with PHP 7.4.6.