We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

$builder->having() not working

I am writing a store locator, but I am having trouble with the HAVING clause. I am spitting out the query, and I keep getting a query without the HAVING clause. Am I missing something?

Here is my code:

                 $query = $this->modelsManager->createBuilder()
                                3959 * acos(
                                    ) * cos(
                                        radians( Stores.latitude )
                                    ) * cos(
                                        radians( Stores.longitude ) - radians('.$area->longitude.')
                                    ) + sin(
                                    ) * sin(
                                        radians( latitude )
                            ) AS distance'))
                        ->orderby('distance DESC')
                        ->having('distance < 1')

Could you please paste the generated query (ie, what is passed to execute()) here?

The error message you are getting would also be helpfull


Here is the query that is generated:

SELECT stores.name AS name, stores.address AS address, (3959 acos(cos(radians(41.967255699999995)) cos(radians(stores.latitude)) cos(radians(stores.longitude) - radians(-87.6577926)) + sin(radians(41.967255699999995)) sin(radians(stores.latitude)))) AS distance FROM stores ORDER BY distance DESC

There is no error thrown by any of this. The problem is there is no HAVING clause being attached to the query.

Unless I am missing something very obvious, your example in the OP does not call having() method.


You are right; I'm sorry about that. I accidentally copied my changes for when I have ->where() instead ->having(). I just checked my code, and re-verified that when ->having() is called, nothing happens.


I put the correct code in the OP, as well.

Erm, but is it legal to have HAVING clause without a GROUP BY clause?


I believe it is, in MySQL; however, I think Phalcon requires a ->groupBy() to add ->having().

Thank you for throwing that out there. That fixed my problem, however. Thank you very much. I was looking at the docs, and didn't see any requirements for it in them. All well!

Thanks again!

yes, it's legal and there are some cases when it's useful

Here is s bit weird (but still reasonable!) example: select count() from someTable having count() > 100 // Get amount of items if there are more than 100, else return nothing

I think when it used without "group by" it should works like regular "where", but this is only my guess.

Dmitry, this is a MySQL extension and it is not protable to other DBMSes.

See, for example, this syntax diagram: http://www.sqlite.org/syntaxdiagrams.html#select-core

I think not, using having without group by is normal in Postgresql which is SQL99-compilant. http://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-HAVING

I think using sqlite.org is not good place where we should look for reference implementation, just look how many useful features they don't implement in sqlite http://www.sqlite.org/omitted.html