2

How can i get the SQL query of this?

$product = Mage::getModel('catalog/product')->load(4329)->getCategoryIds();
Nikitas
  • 1,013
  • 13
  • 27
  • I think it will help you http://stackoverflow.com/questions/2504931/how-do-i-print-all-the-queries-in-magento – ForceMan Dec 01 '13 at 17:10

3 Answers3

8

To enable the sql debugging in magento , open the file lib/Varien/Db/Adapter/Pdo/Mysql.php in your favorite text editor. Down around line 86, you’ll see the following class variables:

    /*
     * Write SQL debug data to file
     *
     * @var bool
     */
    protected $_debug               = false;
    /**
     * Minimum query duration time to be logged
     *
     * @var unknown_type
     */
    protected $_logQueryTime        = 0.05; 
    /**
     * Log all queries (ignored minimum query duration time)
     *
     * @var bool
     */
    protected $_logAllQueries       = false;
    /**
     * Add to log call stack data (backtrace)
     *
     * @var bool
     */
    protected $_logCallStack        = false;
    /**
     * Path to SQL debug data log
     *
     * @var string
     */
    protected $_debugFile           = 'var/debug/sql.txt';

Change the following variables :

 protected $_debug               = true; //false;

And

 protected $_logAllQueries       = true; //false;.

This is all.After running app goto sql.txt you will see all the queries .

Mahmood Rehman
  • 4,303
  • 7
  • 39
  • 76
  • 1
    Will it not work if it's not my favorite editor? How does the computer know what's my favorite one? Stop hacking my brain! Need more foil! Ahh – ahnbizcad Oct 29 '15 at 20:49
  • Great Thank you! On my system, Magento Enterprise 1.14.2, the output is located here: var/debug/pdo_mysql.log. See the $_debugFile variable. – dlink Nov 01 '17 at 19:25
1

echo Mage::getModel('catalog/product')->load(4329)->getCategoryIds()->getSelect();

should work

Munjal
  • 936
  • 1
  • 8
  • 19
0

On Objects of Type Zend_Db_Select (eg Varien_DB_Select) you can call the Method assemble() to get the SQL-Query String.

Bernhard Zürn
  • 584
  • 6
  • 11