doukong1901 2013-07-29 12:56
浏览 32

如何在Magento中禁用SQL Query echo

I have create module for testing in Magento and i call change action url from browser and there SQL query for catalog/product is printed.

I user this code

<?php
class Test_Demo_IndexController extends Mage_Core_Controller_Front_Action
{

public function indexAction()
{
$this->loadLayout();     
$block = $this->getLayout()->createBlock('capacityweb/Test','Test',array('template' => 'capacity/web/test.phtml'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}

public function changeAction()
{
$this->loadLayout();     
$this->renderLayout();

$action=$this->getRequest()->getParam('action');        
$id=$this->getRequest()->getParam('id');


if($action!=null && $id!=null)
{
$relContact = Mage::getModel('catalog/product')->getCollection()->load($id);   
}


}

} 

if i use

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

then magento display SQL query but instead of this

i use

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

then magento not display anything.

So how to fix issue with

$relContact = Mage::getModel('catalog/product')->getCollection()->load($id);   
  • 写回答

2条回答 默认 最新

  • dqsxsmi3704 2013-07-29 13:08
    关注

    This is not an issue.
    Magento uses the Lazy Loading design pattern for collections.
    The sql for a collection is not executed until the collection is loaded or until you iterate through the results.
    So Mage::getModel('catalog/product')->getCollection() will return an object that is an instance of Mage_Catalog_Model_Resource_Product_Collection that you can use how you need, but you don't have the products yet.
    When calling Mage::getModel('catalog/product')->getCollection()->load() (by the way...the $id parameter is useless), then the query SELECT FROM catalog_product_entity..... is exectuted and in $collection->getIterator() you will have the records from the database as instances of Mage_Catalog_Model_Product.
    [EDIT]
    If you want a single product skip the getCollection call. You can get the instance of a product like this

    $product = Mage::getModel('catalog/product')->load($id);
    //changes some attributes
    $product->setData('description', $description);
    //or
    //$product->setDescription($description);
    $product->save().
    

    See more details about it here.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line