Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i run a subquery as a new attribute in a product collection?
    primarykey
    data
    text
    <p>I want to create a new column for the product grid in the admin that counts the amount of children products related to a configurable product in the products grid in the admin.</p> <p>I came up with sub-query to do the calculations:</p> <pre><code>(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) </code></pre> <p>I overrode Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection(), and inserted my attribute using the following code:</p> <pre><code> $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-&gt;addExpressionAttributeToSelect( 'custom_qty', $getChildrenQtySum, 'entity_id' ); </code></pre> <p>The method addExpressionAttributeToSelect() i found in Magento core files, and appears to be able to run raw mysql queries, but when i load the grid page i get a WSOD, and in the log all i get is the query for the products. It looks to me like my sub-query is being wrapped in "`"'s and not using the alias i am specifying (custom_qty).</p> <pre><code>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) </code></pre> <p>What am i doing wrong, and is there a better way to do what i want to accomplish?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload