如何将子查询作为产品集合中的新属性运行


How do i run a subquery as a new attribute in a product collection?

我想为管理员中的产品网格创建一个新列,该列统计与管理员中产品网格中的可配置产品相关的子产品数量。

我想出了子查询来做计算:

(SELECT SUM(children_qtys.qty) FROM (SELECT cisi.qty AS qty FROM catalog_product_super_link cpsa LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpsa.parent_id AND cisi.stock_id=1 WHERE cpsa.parent_id = {{attribute}}) AS children_qtys)

我重写了Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection(),并使用以下代码插入了我的属性:

      $getChildrenQtySum = '(SELECT SUM(children_qtys.qty) FROM (SELECT cisi.qty AS qty FROM catalog_product_super_link cpsa LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpsa.parent_id AND cisi.stock_id=1 WHERE cpsa.parent_id = {{attribute}}) AS children_qtys)';
    $collection->addExpressionAttributeToSelect(
        'custom_qty',
        $getChildrenQtySum,
        'entity_id'
    );

我在Magento核心文件中找到了addExpressionAttributeToSelect()方法,它似乎能够运行原始的mysql查询,但当我加载网格页面时,我得到了一个WSOD,在日志中,我得到的只是产品的查询。在我看来,我的子查询被包装在"`"中,而没有使用我指定的别名(custom_qty)。

a:5:{i:0;s:747:"SELECT `e`.*, (SELECT SUM(children_qtys.qty) FROM (SELECT cisi.qty AS qty FROM catalog_product_super_link cpsa LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpsa.parent_id AND cisi.stock_id=1 WHERE cpsa.parent_id = e.entity_id) AS `children_qtys)`, `at_status`.`value` AS `status`, `at_visibility`.`value` AS `visibility` FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = `e`.`entity_id`) AND (`at_status`.`attribute_id` = '96') AND (`at_status`.`store_id` = 0)
 INNER JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`) AND (`at_visibility`.`attribute_id` = '102') AND (`at_visibility`.`store_id` = 0)

我做错了什么?有没有更好的方法来完成我想完成的事情?

以下是的自定义代码

config.xml 中的调用观察器事件

 <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>                    
                    <NameSpace_Modulename_configurable>
                        <class>NameSpace_Modulename_Model_Observer</class>
                        <method>addConfigureList</method>
                    </NameSpace_Modulename_configurable>
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml>

这是模型文件夹中的Observer.php文件

<?php
class NameSpace_Modulename_Model_Observer
{       

public function addConfigureList(Varien_Event_Observer $observer)
{    
    $block = $observer->getEvent()->getBlock();
    if( ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid)  ) {
        $block->addColumnAfter('configure_list', array(
                'header'    => Mage::helper('modulename')->__('Associated Products'),
                'index'        => 'configure_list',
                'sortable'    => false,
                'filter' => false,
                'width' => '250px',
                'options'    => Mage::getSingleton('productsearchguide/system_config_source_category')->toOptionArrayForAssociated(),
                'renderer'    => 'productsearchguide/catalog_product_grid_render_product',
                'filter_condition_callback' => array($this, 'customfilterCall'),
        ),'name');
    }
}
public function customfilterCall($collection, $column)
    {
        $value = $column->getFilter()->getValue();
        $_product = Mage::getModel('catalog/product')->load($value);
        return $collection;
    }
}

并根据给定的类位置添加该块文件

<?php
 class Namespace_Modulename_Block_Catalog_Product_Grid_Render_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $product = Mage::getModel('catalog/product')->load($row->getEntityId());
        if($product->getTypeId() == 'configurable') {
        $childProducts = Mage::getModel('catalog/product_type_configurable')
                    ->getUsedProducts(null,$product); 
        $allCats = '';
        foreach($childProducts as $key => $child) {
           $allCats .= $child->getName();
            if($key < count($childProducts)-1)
                $allCats.= ',<br />';
           }
           return $allCats;
        }
        return null;
    }
}

根据给定的类位置添加模型文件后

<?php
class Namespace_Modulename_Model_System_Config_Source_Product
{
    public function toOptionArrayForAssociated($addEmpty = true)
    {
        $options = array();
        foreach ($this->loadConfighurableProduct() as $product) {
            $options[$product['value']] =  $product['label'];
        }
        return $options;
    }   
    public function loadConfighurableProduct()
    {
        $store = Mage::app()->getFrontController()->getRequest()->getParam('store', 0);
        $collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('sku')
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('attribute_set_id')
                    ->addAttributeToSelect('type_id'); 
    }
}

享受!!干杯