Magento:如何获取产品负载的 SQL 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9840046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento: How to get SQL query of Product load?
提问by Mukesh Chapagain
What is the full SQL query for product load?
产品加载的完整 SQL 查询是什么?
I mean, how to get the full SQL query for this code:-
我的意思是,如何获取此代码的完整 SQL 查询:-
$productId = 52;
$product = Mage::getModel('catalog/product')->load($productId);
I know, we can get the SQL query for collection object through printLogQuery. Example:-
我知道,我们可以通过 printLogQuery 获取集合对象的 SQL 查询。例子:-
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->printLogQuery(true);
I couldn't find such for product load(). Any idea?
我找不到这样的产品负载()。任何的想法?
回答by ?ukasz L.
load()
is actually the method which execute query on database. For that reason there is no option to "find such for product load()
". Final query is generated before load()
method.
load()
实际上是对数据库执行查询的方法。出于这个原因,没有“为产品找到这样的产品load()
”的选项。最终查询在load()
方法之前生成。
Additionaly, easier method to get query on the screen for example is (on your example):
另外,例如在屏幕上获取查询的更简单方法是(在您的示例中):
$collection = Mage::getModel('catalog/product')->getCollection();
echo $collection->getSelect()->__toString();
回答by Vern Burton
You can dump the actual query via
您可以通过转储实际查询
To enable SQL Debugging, Go to
要启用 SQL 调试,请转到
lib/Varien/Db/Adapter/Pdo/Mysql.php
change line 98
更改第 98 行
protected $_debug = false;
to
到
protected $_debug = true;
It will write the SQL straight to the value of line 126
它会将 SQL 直接写入第 126 行的值
protected $_debugFile = 'var/debug/pdo_mysql.log';
回答by MagePsycho
May be you can benefit from logging all queries for a page load.
Refer the following article for more info:
http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/
也许您可以从记录页面加载的所有查询中受益。
有关更多信息,请参阅以下文章:http:
//blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/
回答by blmage
There's not just one query for the product load.
对产品负载的查询不止一种。
Take a look at the load()
method from the Mage_Eav_Model_Entity_Abstract
class to see how it's working.
查看类中的load()
方法Mage_Eav_Model_Entity_Abstract
,看看它是如何工作的。
回答by Allan MacGregor
Well $product doesn't have a ->getSelect() method but a collection does we can workaround that by doing
好吧,$product 没有 ->getSelect() 方法,但是我们可以通过执行集合来解决这个问题
$product_sku = 'sku';
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('sku', $product_sku);
echo $products->getSelect();
Which will echo
哪个会回声
SELECT
e
.* FROMcatalog_product_entity
ASe
WHERE (e.sku = 'slickerBrush_config')
SELECT
e
.* FROMcatalog_product_entity
ASe
WHERE (e.sku = 'slickerBrush_config')
回答by Abhishek
To see what all queries get fired while calling ->load() method on any modal. Better check into mysql.log file. Check your mysql loggin is enabled or Add this in my.cnf and then restart MySQL server:
查看在任何模式上调用 ->load() 方法时触发的所有查询。最好检查 mysql.log 文件。检查您的 mysql 登录是否已启用或在 my.cnf 中添加此项,然后重新启动 MySQL 服务器:
general_log = 1
log=/var/log/mysqld_query.log
Most of the time these two lines are commented out or not preset better check in other conf also like my.conf, mysql.conf etc. Also time to time empty this file or its size grows very fast might take away most of server memory.
大多数时候这两行被注释掉或没有预设更好地检查其他 conf 也如 my.conf、mysql.conf 等。有时清空这个文件或它的大小增长得非常快可能会占用大部分服务器内存。
Cheers!
干杯!
回答by franzisk
Getting the sql select from
获取 sql 选择
$collection->getSelect()->__toString()
is not reliable. Activate the db log, with n98-magerun.phar
不可靠。使用 n98-magerun.phar 激活数据库日志
n98-magerun.phar dev:log:db
then you can tail the log file
然后你可以拖尾日志文件
tail -f var/debug/pdo_mysql.log
to see realtime executing queries.
When you are done, deactivate the db log, using the same command
查看实时执行的查询。
完成后,使用相同的命令停用数据库日志
n98-magerun.phar dev:log:db