Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpactiverecord - display generated query?

Using phpactiverecord from http://www.phpactiverecord.org/, is there any way to see the generated SQL query for debugging, other than viewing the database server's query log?

like image 760
user1977661 Avatar asked Mar 26 '13 10:03

user1977661


3 Answers

Within your Model:

echo self::connection()->last_query;

Outside your Model:

YourModelName::connection()->last_query;
like image 78
kaizenCoder Avatar answered Nov 02 '22 05:11

kaizenCoder


A couple of different ways to get the SQL statement without the parameters:

  1. static::table()->conn->last_query #returns last query to the connection
  2. static::connection()->last_query #same as the first
  3. static::table()->last_sql #this will only return the last query sent to the finder functions and will not include association queries

To get the full statement with parameters set, you'll need to role your own function (see php.net comment) and insert it into the php-activerecord/lib/Connection.php::query function. Likely not a great idea for production.

like image 27
spyle Avatar answered Nov 02 '22 04:11

spyle


In a class extending ActiveRecord\Model, the last query is in ClassName::connection()->last_query. e.g.

class Todo extends ActiveRecord\Model {
    public static function test() {
        Todo::all();
        var_dump(Todo::connection()->last_query);       
    }
}
like image 33
user1977661 Avatar answered Nov 02 '22 03:11

user1977661