{"id":6549,"date":"2014-04-20T08:49:29","date_gmt":"2014-04-20T08:49:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/add-coun-expression-in-join-to-magento-collection-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:50:18","modified_gmt":"2022-08-30T15:50:18","slug":"add-coun-expression-in-join-to-magento-collection-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/add-coun-expression-in-join-to-magento-collection-collection-of-common-programming-errors\/","title":{"rendered":"Add Coun Expression in Join to Magento Collection-Collection of common programming errors"},"content":{"rendered":"<p>Hi guys I&#8217;m having a hard time adding a join in Magento, well I know I can do <code>$collection-&gt;getSelect()-&gt;join<\/code> but I need to add a <code>Count()<\/code> expression in it.<\/p>\n<p>What I&#8217;m concretly trying to achieve is this query:<\/p>\n<pre><code>SELECT `e`.*, COUNT(sfoi.product_id) as `count`\nFROM `catalog_product_entity` \nAS `e` \nLEFT JOIN sales_flat_order_item sfoi\nON sfoi.product_id = e.entity_id \nORDER BY `e`.`entity_id` asc, \n`e`.`entity_id` asc \n<\/code><\/pre>\n<p>Edit:<\/p>\n<p>THis is what it looks like now:<\/p>\n<pre><code>$e = Mage::getModel('catalog\/product')-&gt;getCollection()-&gt;getSelect();\n$e-&gt;join(array('c' =&gt; 'sales_flat_order_item'),\n'e.entity_id = c.product_id',\narray('count' =&gt; 'COUNT(c.product_id)'));\n<\/code><\/pre>\n<p>Outputting this gives:<\/p>\n<pre><code>SELECT `e`.*, COUNT(c.product_id) AS `count` FROM `catalog_product_entity` \nAS `e` INNER         \nJOIN `sales_flat_order_item` AS `c` ON e.entity_id = c.product_id\n<\/code><\/pre>\n<p>doing this in my collection crashes the adminhtml grid. Tracing the reports it crashes here:<\/p>\n<p><code>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-&gt;__call('setPageSize', Array)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi guys I&#8217;m having a hard time adding a join in Magento, well I know I can do $collection-&gt;getSelect()-&gt;join but I need to add a Count() expression in it. What I&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,1],"tags":[],"class_list":["post-6549","post","type-post","status-publish","format-standard","hentry","category-magento","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6549"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6549\/revisions"}],"predecessor-version":[{"id":9014,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6549\/revisions\/9014"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}