Add Coun Expression in Join to Magento Collection-Collection of common programming errors
Hi guys I’m having a hard time adding a join in Magento, well I know I can do $collection->getSelect()->join
but I need to add a Count()
expression in it.
What I’m concretly trying to achieve is this query:
SELECT `e`.*, COUNT(sfoi.product_id) as `count`
FROM `catalog_product_entity`
AS `e`
LEFT JOIN sales_flat_order_item sfoi
ON sfoi.product_id = e.entity_id
ORDER BY `e`.`entity_id` asc,
`e`.`entity_id` asc
Edit:
THis is what it looks like now:
$e = Mage::getModel('catalog/product')->getCollection()->getSelect();
$e->join(array('c' => 'sales_flat_order_item'),
'e.entity_id = c.product_id',
array('count' => 'COUNT(c.product_id)'));
Outputting this gives:
SELECT `e`.*, COUNT(c.product_id) AS `count` FROM `catalog_product_entity`
AS `e` INNER
JOIN `sales_flat_order_item` AS `c` ON e.entity_id = c.product_id
doing this in my collection crashes the adminhtml grid. Tracing the reports it crashes here:
a:5:{i:0;s:35:"Unrecognized method 'setPageSize()'";i:1;s:4771:"#0 /app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(504): Zend_Db_Select->__call('setPageSize', Array)