Yii Framework Forum: How to ECHO or PRINT SQL query - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to ECHO or PRINT SQL query Rate Topic: -----

#1 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 708
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 18 June 2009 - 10:03 AM

hello,

I tried to find a simple way to echo the actual SQL query that I'm trying to execute. (for testing/checking purposes)

something like:
User::Model()->getQueryString() 


but no luck :(

thanks,
--iM
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 18 June 2009 - 10:14 AM

maybe you set enableProfiling = true in your config (db)
then add a CProfileLogRoute as a route for the CLogRouter

this dumps all your queries at the bottom of your page
0

#3 User is offline   eval 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-November 08
  • Location:Athens - Greece

Posted 18 June 2009 - 11:44 AM

Add trace level in your log configuration inside protected/config/main.php

Or in case you want a mysql query trace (and ofc you use mysql), add:

log = /tmp/mysql.log


to your my.cnf file and restart mysql daemon.

I think both will do.

0

#4 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 708
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 18 June 2009 - 12:20 PM

hello,

so this is not exactly what I was looking for, but it will do for now. (it's kinda weird that Yii doesn't have a simple solution for this)


so my main.php looks like this:
    'components'=>array(
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),

                array(
                    'class'=>'CWebLogRoute',
                    'levels' => 'trace',
                ),
            ),
        ),


and a little bit below ...

 'db'=>array(
            'class'=>'CDbConnection',

            .
            .
            .

            'enableProfiling'=>true
        ),


and this will display everything on the bottom part of the screen

thanks everybody for the help,

--iM
0

#5 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 18 June 2009 - 12:44 PM

wow works perfectly!  :o

Nice feature I didn't know.
0

#6 User is offline   Alex Muir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 25-April 09

Posted 18 June 2009 - 01:58 PM

My log is giving me SQL queries before the parameters have been replaced.  Eg.

Executing SQL: INSERT INTO `Slug` (`ownerId`, `ownerTable`, `slug`) VALUES
(:yp0, :yp1, :yp2)


AND

Querying SQL: SELECT COUNT(*) FROM `Slug` WHERE LOWER(slug)=?


Is there any way to see them with the parameters actually within the query?  It makes it kind of hard to see what's going on.
0

#7 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 June 2009 - 02:48 PM

You may configure your db connection with enableParamLogging=true.

You should turn off this in production, however, because it degrades your overall performance.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users