嗨,大家好,我很难在Magento中加入连接,我知道我可以做$collection->getSelect()->join
但我需要在其中添加Count()
表达式。在Magento Collection中加入Coun表达式
什么我较具体来说想要实现这个查询:
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
编辑:
这是它看起来像现在:
$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)'));
1.4.3这给:
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
在我的集合中这样做会导致adminhtml网格崩溃。 跟踪报告崩溃的位置:
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)