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

Get SQL Query String with condition & bind parameter.

Hi,

I am new in phalcon. I am trying to debug SQL Query string but I am unable to find out how i can do it ??

Can you help me plz.

Ex: Users::find('is_active="1"') I want query like : "Select * from users where is_active='1'";

In Laravel I can do it by using ->getSql() but in phalcon how i cn do it??

Thanks .

edited Apr '16

https://docs.phalcon.io/en/latest/reference/phql.html https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html

From query builder you can call

// Returns a PHQL statement built based on the builder parameters
$phql = $builder->getPhql();

// Returns the query built
$query = $builder->getQuery();

Find method are returning direct results, you cant get query from it. You can do it if you implement events manager for DB adapter

https://docs.phalcon.io/en/latest/reference/events.html

Hi , Thank you for reply , but I don't want to do with "modelsManager".

I have some relation in model , and on the basic of that realtion I want to know the exact query is running to get result .

and some where i have join query. and want to debug ...

https://docs.phalcon.io/en/latest/reference/phql.html https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html

From query builder you can call

// Returns a PHQL statement built based on the builder parameters
$phql = $builder->getPhql();

// Returns the query built
$query = $builder->getQuery();

Find method are returning direct results, you cant get query from it. You can do it if you implement events manager for DB adapter

https://docs.phalcon.io/en/latest/reference/events.html

I've found the easiest solution is to simply turn on the general query log in Mysql, then do a tail -f /path/to/log/file. That way it doesn't matter how the query is built, you can see the actual text of the query that MySQL sees.