我想在管理员中为产品网格创建一个新列,该列计算与管理员产品网格中可配置产品相关的子产品数量。
我想出了子查询来进行计算:
(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)
我做错了什么,有没有更好的方法来做我想要完成的事情?