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

Is there way to get SQL or PHQL query string from Phalcon\Mvc\Model\Criteria ?

Hello!

Another question :)

Is there way to get SQL or PHQL query string from Phalcon\Mvc\Model\Criteria ?

For example:

$content = new users();          
$criteria = $content->query();

How to get string "SELECT * FROM users" from $criteria ?

P.S. Yes, I want to use \Phalcon as query builder ;)

You can implement a logger: https://docs.phalcon.io/en/latest/reference/models.html#logging-low-level-sql-statements.

However, I just ended up turning on the MySQL general query log and tailing that file.



36.0k
edited Oct '14

I can use logger after querying database, but I need to build the query with OO and execute the query with PDO or smthng else.

There is way to get SQL query:

Class MySqlBuilder extends \Phalcon\Mvc\Model\Query\Builder
{
       public function getParams()
       {
           return $this->_bindParams;
       }
       public function getSQL()
       {
                    $queryS = $this->getPhql();
                    $queryS = str_replace(array("[", "]"), "", $queryS);
                    $queryS = str_replace(":,", ",", $queryS);
                    $queryS = str_replace(":)", ")", $queryS);
                    return queryS 
        }
}

Than insert MySQLBuilder to DI...

Is there more beautiful way?

edited Nov '14

I do not understand how to insert here the code, you can read it there