<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Musings of ErisDS &#187; Magento</title>
	<atom:link href="http://erisds.co.uk/category/magento/feed" rel="self" type="application/rss+xml" />
	<link>http://erisds.co.uk</link>
	<description>Web development, Symfony, Wordpress and general geekery</description>
	<lastBuildDate>Sat, 07 Jan 2012 10:29:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Snippet: Magento &#8211; Using OR and LEFT JOIN in addAttributeToFilter</title>
		<link>http://erisds.co.uk/magento/snippet-magento-using-or-and-left-join-in-addattributetofilter</link>
		<comments>http://erisds.co.uk/magento/snippet-magento-using-or-and-left-join-in-addattributetofilter#comments</comments>
		<pubDate>Thu, 29 Jul 2010 11:02:52 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=1496</guid>
		<description><![CDATA[I'm still pretty new to Magento, and am learning more about it every day. I've been trying to learn all I can about internals such as managing collections &#038; recently stumbled on trying to filter a collection by two attributes. I wanted to build a list of all products that had one OR another attribute set to certain values.


Related posts:<ol><li><a href='http://erisds.co.uk/symfony/snippet-symfony-forms-accessing-the-object-in-a-form' rel='bookmark' title='Permanent Link: Snippet: Symfony Forms &#8211; Accessing the Object in a Form'>Snippet: Symfony Forms &#8211; Accessing the Object in a Form</a><small>Perusing my Google Analytics data shows up some interesting results....</small></li>
<li><a href='http://erisds.co.uk/symfony/snippet-creating-debugging-complex-sql-queries-in-symfony' rel='bookmark' title='Permanent Link: Snippet: Creating &#038; Debugging Complex SQL queries in Symfony'>Snippet: Creating &#038; Debugging Complex SQL queries in Symfony</a><small>When working with large and complex datasets in Symfony, there...</small></li>
<li><a href='http://erisds.co.uk/symfony/snippet-symfony-forms-allowing-extra-fields' rel='bookmark' title='Permanent Link: Snippet: Symfony Forms &#8211; Allowing Extra Fields'>Snippet: Symfony Forms &#8211; Allowing Extra Fields</a><small>Place this line of code in the configure method of...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still pretty new to Magento, and am learning more about it every day. I&#8217;ve been trying to learn all I can about internals such as managing collections &#038; recently stumbled on trying to filter a collection by two attributes. I wanted to build a list of all products that had one OR another attribute set to certain values.</p>
<p>I found lots of documentation on <kbd>addAttributeToFilter</kbd> that showed how to add several filters creating an &#8220;AND&#8221; relationship and also examples how to test if a single attribute had one value or another. Perhaps it&#8217;s just me, but I always find it hard to find documentation on how to do anything more complex than the basics in Magento!</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _prepareCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000088;">$collection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'catalog/product'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCollection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000088;">$collection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttributeToFilter</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'attribute'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'my_attribute'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'eq'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'attribute'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'my_other_attribute'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'neq'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'left'</span><br />
&nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>After a bit of playing I found you can pass nested arrays with different attribute values to create the &#8220;OR&#8221; relationship in the generated SQL. To get this working correctly I also had to pass &#8220;left&#8221; as the final parameter to make Magento use left joins rather than cross joins. It&#8217;s neat and tidy once you know it, but a bit awkward to figure out if you don&#8217;t!</p>
<p class="small"><strong>A Note on Snippets:</strong> To me Magento is a mind boggling world of abstraction, configuration and depths of potential knowledge. I find the documentation less than helpful and it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.</p>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/symfony/snippet-symfony-forms-accessing-the-object-in-a-form' rel='bookmark' title='Permanent Link: Snippet: Symfony Forms &#8211; Accessing the Object in a Form'>Snippet: Symfony Forms &#8211; Accessing the Object in a Form</a><small>Perusing my Google Analytics data shows up some interesting results....</small></li>
<li><a href='http://erisds.co.uk/symfony/snippet-creating-debugging-complex-sql-queries-in-symfony' rel='bookmark' title='Permanent Link: Snippet: Creating &#038; Debugging Complex SQL queries in Symfony'>Snippet: Creating &#038; Debugging Complex SQL queries in Symfony</a><small>When working with large and complex datasets in Symfony, there...</small></li>
<li><a href='http://erisds.co.uk/symfony/snippet-symfony-forms-allowing-extra-fields' rel='bookmark' title='Permanent Link: Snippet: Symfony Forms &#8211; Allowing Extra Fields'>Snippet: Symfony Forms &#8211; Allowing Extra Fields</a><small>Place this line of code in the configure method of...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/magento/snippet-magento-using-or-and-left-join-in-addattributetofilter/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

