{"id":70,"date":"2010-11-23T05:32:10","date_gmt":"2010-11-23T05:32:10","guid":{"rendered":"http:\/\/www.evermoredevnotes.com\/?p=70"},"modified":"2010-11-23T05:32:10","modified_gmt":"2010-11-23T05:32:10","slug":"updating-a-magento-collection-with-zend_db-objects","status":"publish","type":"post","link":"https:\/\/www.evermoretechnologies.com\/blog\/2010\/11\/updating-a-magento-collection-with-zend_db-objects\/","title":{"rendered":"Updating a Magento Collection with Zend_db objects."},"content":{"rendered":"<p>So, I needed to update a magento adminhtml grid.\u00a0 I took a look at the database and it seemed pretty simple.\u00a0 There is a table with all of the main information, and a history table that is referenced when an addHistory function is called on the collection.<\/p>\n<p>The old add history function looked like this.<\/p>\n<p>[sourcecode language=&#8221;php&#8221;]<br \/>\npublic function addHistory()<br \/>\n    {<br \/>\n        if ($this-&gt;_hasHistory) {<br \/>\n            return $this;<br \/>\n        }<br \/>\n        $this-&gt;_hasHistory = true;<br \/>\n\t$this-&gt;getSelect()-&gt;join(array(&#8216;h&#8217;=&gt;$this-&gt;getTable(&#8216;ugiftcert\/history&#8217;)), &#8216;h.cert_id=main_table.cert_id&#8217;, array(&#8216;ts&#8217;, &#8216;amount&#8217;, &#8216;customer_id&#8217;, &#8216;customer_email&#8217;, &#8216;order_id&#8217;, &#8216;order_increment_id&#8217;, &#8216;order_item_id&#8217;, &#8216;user_id&#8217;, &#8216;username&#8217;))<br \/>\n            -&gt;where(&quot;h.action_code=&#8217;create&#8217;&quot;);<br \/>\n        return $this;<br \/>\n    }<br \/>\n[\/sourcecode]<\/p>\n<p>The history table had action_code of create and update, and I needed to bring in the date for the update actions where they existed. Since updates always happen after create, I figured I could take the max of the ts column, which is the date column, where action code is create or update.<\/p>\n<p>I did some documentation searching and found several magento collection functions that kept throwing &#8220;method not found&#8221; errors including groupByAttribute to name one. I was close, but it wasn&#8217;t quite working right. Then after consulting a colleague, Joseph Piche, he noticed that it was not a magento collection object I was trying to change, but a Zend_Db object. I had the function mostly working, but couldn&#8217;t get the group by in my sql to work correctly. Turns out, the method I wanted was named simply &#8220;group.&#8221; With this information, the update became quite trivial and my final function looks like this:<\/p>\n<p>[sourcecode language=&#8221;php&#8221;]<br \/>\npublic function addHistory()<br \/>\n    {<br \/>\n        if ($this-&gt;_hasHistory) {<br \/>\n            return $this;<br \/>\n        }<br \/>\n        $this-&gt;_hasHistory = true;<br \/>\n\t$this-&gt;getSelect()-&gt;join(array(&#8216;h&#8217;=&gt;$this-&gt;getTable(&#8216;ugiftcert\/history&#8217;)), &#8216;h.cert_id=main_table.cert_id&#8217;, array(&#8216;amount&#8217;, &#8216;customer_id&#8217;, &#8216;customer_email&#8217;, &#8216;order_id&#8217;, &#8216;order_increment_id&#8217;, &#8216;order_item_id&#8217;, &#8216;user_id&#8217;, &#8216;username&#8217;))<br \/>\n            -&gt;from(null, array(&#8216;ts&#8217; =&gt; &#8216;MAX(ts)&#8217;))<br \/>\n\t    -&gt;group(array(&#8216;main_table.cert_id&#8217;))<br \/>\n            -&gt;where(&quot;h.action_code=&#8217;create&#8217; or h.action_code=&#8217;update&#8217;&quot;);<br \/>\n        return $this;<br \/>\n    }<br \/>\n[\/sourcecode]<\/p>\n<p>Lesson learned: Know the object you&#8217;re trying to modify before using methods on it. A simple look at the class inheritance which shows:<\/p>\n<p>[sourcecode language=&#8221;php&#8221;]<br \/>\nclass Unirgy_GiftCert_Model_Mysql4_Cert_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract<br \/>\n[\/sourcecode]<\/p>\n<p>and I would have known where to search for methods to make my life a lot easier while searching for the group by method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I needed to update a magento adminhtml grid.\u00a0 I took a look at the database and it seemed pretty simple.\u00a0 There is a table with all of the main&#8230; <a href=\"https:\/\/www.evermoretechnologies.com\/blog\/2010\/11\/updating-a-magento-collection-with-zend_db-objects\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,22],"tags":[],"_links":{"self":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":0,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}