如何在magento中打印集合mysql查询

时间:2012-04-04 16:34:45

标签: debugging magento

假设我有一个类似的集合:

$products = Mage::getModel('catalog/product')
            ->getCollection()
            ...
            ->load();

如何打印执行的实际MySQL代码?

10 个答案:

答案 0 :(得分:20)

您可以随时通过回显getSelect来查看您的SQL查询,如下所示:

$products = Mage::getModel('catalog/product')
        ->getCollection();
echo $products->getSelect();

要更改查询参数,请查看以下方法:

$products->addAttributeToSelect('someattribute');
$products->addAttributeToFilter('someattribute', array('eq'=>'1'));

答案 1 :(得分:13)

您可以使用以下代码打印集合: 我们可以使用getSelect()->__toString()

打印集合查询
$products = Mage::getModel(‘catalog/product’)
 ->addAttributeToFilter(‘status’, array(‘eq’ => 1));
echo $products->getSelect()->__toString();

你见过http://kuldipchudasama.wordpress.com/2012/07/16/magento-print-query-of-collection/吗?     这很有效。

答案 2 :(得分:6)

此处的大多数其他答案都表示$products->getSelect()会这样做 - 如果您要处理的只是echo,这很好,但事实上getSelect()没有&# 39; t只返回一个字符串,它返回一个Varien_Db_Select对象。

在该对象上调用echo会自动触发其__toString()方法,因此您只需获取SQL字符串,但尝试将其传递给Mage::log(),您将获得更多比你预期的要好。

如果您只想记录SQL,可以使用:

Mage::log($products->getSelect()->__toString());

或者如何使用对象自己:

$products->printLogQuery(false, true); // don't echo, do log

printLogQuery在lib / Varien / Data / Collection / Db.php中定义。

答案 3 :(得分:2)

您可以打印

$products->getSelect()->assemble();

答案 4 :(得分:2)

如果你简单地将->load()的第一个参数设置为true,就像这样:

$products = Mage::getModel('catalog/product')
            ->getCollection()
            ...
           ->load(true);

答案 5 :(得分:2)

我每天都在收藏品。毫无疑问,这是正确的方法。

echo $collection->getSelectSql(true);

答案 6 :(得分:0)

步骤1-

$result_colletion = print_r($collection->getSelect()); 
Mage::log($$result_colletion, null, custom_collection.log,true);

第2步

之后登录magento admin部分并启用日志设置。请参阅下文。

System > Configuration > Developer > Log Settings

第3步 -

之后,请参阅custom_collection.log文件夹中的日志文件var/log/

答案 7 :(得分:0)

在Magento 2中: -

namespace <Company>\<Module>\Block\Adminhtml\Tab\Log;
class Grid 
extends \Magento\Backend\Block\Widget\Grid\Extended
{

    protected $_collectionFactory;

    /**
     * Constructor
     *
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \<Company>\<Module>\Model\ResourceModel\Log\CollectionFactory $collectionFactory
     * @param Psr\Log\LoggerInterface $logger
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \<Company>\<Module>\Model\ResourceModel\Log\CollectionFactory $collectionFactory,
        \Psr\Log\LoggerInterface $logger,
        array $data = []
    ) {
        $this->_logger = $logger;
        $this->_collectionFactory = $collectionFactory;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * {@inheritdoc}
     */
    protected function _prepareCollection()
    {
        $collection = $this->_collectionFactory->create();
        $this->_logger->info($collection->getSelect()->__toString());
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
}

请记住,收集工厂是一个魔法类,可以附加到每个类,因为Magento 1不够复杂。

答案 8 :(得分:0)

尝试以下代码。

 $products = Mage::getModel('catalog/product')
                ->getCollection();

    echo $products->getSelect();

答案 9 :(得分:-3)

$ collection =“Sql Query Here”;
echo $ collection-&gt; getSelect() - &gt; __ toString();

Sql查询产品目录

选择e.entity_id,IF(at_name.value_id&gt; 0,at_name.value,at_name_default.value)AS名称,pcategory_name.value AS'PCat',eaov.value AS'品牌',stoke.stock_status AS'inStoke ',pcategory_name.entity_id AS'cat_id',ccei.value AS'ave_active'FROM catalog_product_entity AS e INNER JOIN catalog_product_entity_varchar AS at_name_default ON(at_name_default.entity_id = e.entity_id)AND(at_name_default.attribute_id =(SELECT attribute_id FROM eav_attribute ea LEFT JOIN eav_entity_type et ON ea.entity_type_id = et.entity_type_id WHERE ea.attribute_code ='name'AND et.entity_type_code ='catalog_product'))AND at_name_default.store_id = 0 LEFT JOIN catalog_product_entity_varchar AS at_name ON(at_name.entity_id = e.entity_id)AND (at_name.attribute_id =(SELECT attribute_id FROM eav_attribute ea LEFT JOIN eav_entity_type et ON ea.entity_type_id = et.entity_type_id WHERE ea.attribute_code ='name'AND et.entity_type_code ='catalog_product'))AND(at_name.store_id = 1)LEFT加入cata loginventory_stock_status AS stoke ON(stoke.product_id = e.entity_id)LEFT JOIN catalog_product_entity_varchar AS键ON(key.entity_id = e.entity_id)AND key.attribute_id = 160 INNER JOIN catalog_category_entity AS pcategory ON pcategory.entity_id =(SELECT MAX(category_id) FROM catalog_category_product WHERE product_id = e.entity_id AND category_id!= 2)INNER JOIN catalog_category_entity_int AS ccei ON ccei.entity_id = pcategory.entity_id AND ccei.attribute_id = 34 LEFT JOIN catalog_category_entity_varchar AS pcategory_name ON pcategory_name.entity_id = pcategory.entity_id AND pcategory_name.attribute_id = 33 LEFT JOIN catalog_product_entity_int AS cpei ON cpei.entity_id = e.entity_id AND cpei.entity_type_id = 4 AND cpei.attribute_id = 70 LEFT JOIN catalog_product_entity_int AS cpeis ON cpeis.entity_id = e.entity_id AND cpeis.attribute_id = 155 LEFT JOIN eav_attribute_option_value AS eaov ON eaov.option_id = cpei.value WHERE ccei.value = 1 AND pcategory.parent_id = 2