Jono B Fri Oct 22 15:09:21 -0400 2010

Subject: Sql string

If possible, I would like to examine the sql string that php.activerecord generates. Where is the best place to intercept this?


Benjamin P Sat Oct 23 06:40:10 -0400 2010

There is a functionality called Logging, but I didn't find out yet where the Log class comes from - see unit tests.

Jacques Fuentes Sat Oct 23 17:23:50 -0400 2010

Here's an example of logging:

 1 require_once 'php-activerecord/ActiveRecord.php';
 2 
 3 ActiveRecord\Config::initialize(function($cfg)
 4 {
 5   // normal cfg settings above
 6   $logger = Log::singleton('file','/path/to/app/logs/phpar.log','ident',array('mode' => 0664, 'timeFormat' =>  '%Y-%m-%d %H:%M:%S'));
 7 
 8   $cfg->set_logging(true);
 9   $cfg->set_logger($logger);
10 });
Benjamin P Sun Oct 24 04:53:56 -0400 2010

Oh, I see, it uses the pear package:

http://pear.php.net/package/Log/redirected
or
pear install Log

Jono B Tue Oct 26 07:23:30 -0400 2010

Awesome sauce, thank you.

For those who need a step-by-step (my local dev is xampp on windows, so YMMV):

1. Make sure you have pear installed and working
2. To install the Log package from pear:
2.1. Open up a command promt
2.2. Navigate to your php directory in xampp (mine is E:\xampp\php)
2.3 Type "pear list" and check if Log is installed.
2.4 If Log is not installed, then type "pear install Log". You should get some messages in the cmd console that "install ok"
3. To use Log, follows Jacques code above, but also make sure that you include the Log.php file from the pear package:

 1  require_once 'php-activerecord/ActiveRecord.php';
 2  require_once 'E:\xampp\php\PEAR\Log.php';
 3 
 4  ActiveRecordConfig::initialize(function($cfg)
 5  {
 6    // normal cfg settings above
 7    $logger = Log::singleton('file','/path/to/app/logs/phpar.log','ident',array('mode' => 0664, 'timeFormat' =>  '%Y-%m-%d %H:%M:%S'));
 8 
 9    $cfg->set_logging(true);
10    $cfg->set_logger($logger); }); 

For more information, also see http://www.indelible.org/php/Log/guide.html

Jono B Tue Oct 26 07:36:39 -0400 2010

Jacques,

Quick question: at the moment it seems that logging happens before substiution in my where clause. So something like:
'conditions' => array('account_id = ?, $id)
is logged as
"WHERE account_id = ?"

Is it possible to set the logging after variable substitution?

Alexei Berkau Wed Nov 17 09:06:16 -0500 2010

exactly the same question

Benjamin P Wed Nov 17 13:06:30 -0500 2010

These are prepared statements, so you cannot log the final statement within PHP. Instead, you can use the Mysql Server Query log:

http://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/
http://dev.mysql.com/doc/refman/5.1/en/query-log.html

Alternatively you could change Connection.php:273 to log the values (here: $id) as well.

Dmitry Kuznetsov Wed May 18 04:59:04 -0400 2011

Guys is there any way to get query execution time using pear log?

Yoan B Wed May 18 15:55:37 -0400 2011

Sure, but not using the vanilla version.

https://github.com/greut/php-activerecord/compare/gh133-profiling

Dmitry Kuznetsov Wed May 18 16:40:07 -0400 2011

Oh! Thank you so much! This one works perfectly!

(1-10/10)