You are viewing an old revision of this post, from April 12, 2017 @ 17:38:43. See below for differences between this version and the current revision.

Magento – ADDATTRIBUTETOFILTER CONDITIONALS

addAttributeToFilter is a method that can be called on EAV collections in Magento. This includes product collections, category collections, customer collections and many more. In short, it adds a condition to the WHERE part of the MySQL query used to extract a collection from the database, therefore allowing you to filter the collection by custom conditions.

This article will focus on product collections in Magento but all of the addAttributeToFilter calls can be used on any EAV collection in Magento.

$_products = Mage::getResourceModel('catalog/product_collection')
 ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
 ->addAttributeToFilter('sku', array('like' => 'UX%'))
 ->load();

The above code would get a product collection, with each product having it's name, url, price and small image loaded in it's data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

addAttributeToFilter Conditionals

Notice above that I used the LIKE operator? There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.

Equals: eq

This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you're using.

$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator

Not Equals - neq

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

Like - like

$_products->addAttributeToFilter('sku', array('like' => 'UX%'));

One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.

Not Like - nlike

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));

In - in

$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));

When using in, the value parameter accepts an array of values.

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));

NULL - null

$_products->addAttributeToFilter('description', array('null' => true));

Not NULL - notnull

$_products->addAttributeToFilter('description', array('notnull' => true));

Greater Than - gt

$_products->addAttributeToFilter('id', array('gt' => 5));

Less Than - lt

$_products->addAttributeToFilter('id', array('lt' => 5));

Greater Than or Equals To- gteq

$_products->addAttributeToFilter('id', array('gteq' => 5));

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));

addFieldToFilter()

addAttributeToFilter only works with EAV entity types (products, categories, customers etc) in Magento. To use the same functionality with non-eav entity models, swap the addAttributeToFilter() method with addFieldToFilter(). This functions works in the exact same way and takes the same parameters, however it works on non-EAV entities.

Debugging The SQL Query

There are two ways to debug the query being executed when loading a collection in Magento.

// Method 1
Mage::getResourceModel('catalog/product_collection')->load(true);

// Method 2
$collection = Mage::getResourceModel('catalog/product_collection')

echo $collection->getSelect();

Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much quicker but I include them both here for reference.

Revisions

  • April 12, 2017 @ 17:39:31 [Current Revision] by Sharing Solution
  • April 12, 2017 @ 17:39:31 by Sharing Solution
  • April 12, 2017 @ 17:38:43 by Sharing Solution

Revision Differences

April 12, 2017 @ 17:38:43Current Revision
Content
Unchanged: <p>Unchanged: <p>
Deleted: addAttributeToFilter is a method that can be called on EAV collections in Magento. This includes product collections, category collections, customer collections and many more. In short, it adds a condition to the WHERE part of the MySQL query used to extract a collection from the database, therefore allowing you to filter the collection by custom conditions. Mastering this function is key if you want to learn to write great code and great&nbsp;<a href="https:/ /fishpig.co.uk/ magento/extensions/">Magento extensions</a>. Added: addAttributeToFilter is a method that can be called on EAV collections in Magento. This includes product collections, category collections, customer collections and many more. In short, it adds a condition to the WHERE part of the MySQL query used to extract a collection from the database, therefore allowing you to filter the collection by custom conditions.
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: This article will focus on product collections in Magento but all of the addAttributeToFilter calls can be used on any EAV collection in Magento.Unchanged: This article will focus on product collections in Magento but all of the addAttributeToFilter calls can be used on any EAV collection in Magento.
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products = Mage::getResourceModel( &#39;catalog/ product_collection&#39;)Unchanged: <code>$_products = Mage::getResourceModel( &#39;catalog/ product_collection&#39;)
Unchanged: -&gt;addAttributeToSelect( array(&#39;name&#39;, &#39;product_url&#39;, &#39;small_image&#39;))Unchanged: -&gt;addAttributeToSelect( array(&#39;name&#39;, &#39;product_url&#39;, &#39;small_image&#39;))
Unchanged: -&gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;like&#39; =&gt; &#39;UX%&#39;))Unchanged: -&gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;like&#39; =&gt; &#39;UX%&#39;))
Unchanged: -&gt;load();< /code></pre>Unchanged: -&gt;load();< /code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: The above code would get a product collection, with each product having it&#39;s name, url, price and small image loaded in it&#39;s data array. The product collection would be filtered and contain only products that have an SKU starting with UX.Unchanged: The above code would get a product collection, with each product having it&#39;s name, url, price and small image loaded in it&#39;s data array. The product collection would be filtered and contain only products that have an SKU starting with UX.
Unchanged: </p>Unchanged: </p>
Unchanged: <h3>Unchanged: <h3>
Unchanged: addAttributeToFilter ConditionalsUnchanged: addAttributeToFilter Conditionals
Unchanged: </h3>Unchanged: </h3>
Unchanged: <p>Unchanged: <p>
Unchanged: Notice above that I used the LIKE operator? There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.Unchanged: Notice above that I used the LIKE operator? There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.
Unchanged: </p>Unchanged: </p>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Equals: eqUnchanged: Equals: eq
Unchanged: </h4>Unchanged: </h4>
Unchanged: <p>Unchanged: <p>
Unchanged: This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you&#39;re using.Unchanged: This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you&#39;re using.
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;status&#39;, array(&#39;eq&#39; =&gt; 1)); // Using the operatorUnchanged: <code>$_products- &gt;addAttributeToFilter( &#39;status&#39;, array(&#39;eq&#39; =&gt; 1)); // Using the operator
Unchanged: $_products-&gt; addAttributeToFilter(&#39; status&#39;, 1); // Without using the operator</code></pre>Unchanged: $_products-&gt; addAttributeToFilter(&#39; status&#39;, 1); // Without using the operator</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Not Equals - neqUnchanged: Not Equals - neq
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;neq&#39; =&gt; &#39;test-product&#39; ));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;neq&#39; =&gt; &#39;test-product&#39; ));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Like - likeUnchanged: Like - like
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;like&#39; =&gt; &#39;UX%&#39; ));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;like&#39; =&gt; &#39;UX%&#39; ));</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.Unchanged: One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.
Unchanged: </p>Unchanged: </p>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Not Like - nlikeUnchanged: Not Like - nlike
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;nlike&#39; =&gt; &#39;err-prod%&#39; ));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;sku&#39;, array(&#39;nlike&#39; =&gt; &#39;err-prod%&#39; ));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: In - inUnchanged: In - in
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;in&#39; =&gt; array(1,4,98) ));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;in&#39; =&gt; array(1,4,98) ));</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: When using in, the value parameter accepts an array of values.Unchanged: When using in, the value parameter accepts an array of values.
Unchanged: </p>Unchanged: </p>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Not In - ninUnchanged: Not In - nin
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;nin&#39; =&gt; array(1,4,98) ));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;nin&#39; =&gt; array(1,4,98) ));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: NULL - nullUnchanged: NULL - null
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;description&#39;, array(&#39;null&#39; =&gt; true));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;description&#39;, array(&#39;null&#39; =&gt; true));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Not NULL - notnullUnchanged: Not NULL - notnull
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;description&#39;, array(&#39;notnull&#39; =&gt; true));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;description&#39;, array(&#39;notnull&#39; =&gt; true));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Greater Than - gtUnchanged: Greater Than - gt
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;gt&#39; =&gt; 5));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;gt&#39; =&gt; 5));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Less Than - ltUnchanged: Less Than - lt
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;lt&#39; =&gt; 5));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;lt&#39; =&gt; 5));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Greater Than or Equals To- gteqUnchanged: Greater Than or Equals To- gteq
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;gteq&#39; =&gt; 5));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;gteq&#39; =&gt; 5));</code></pre>
Unchanged: <h4>Unchanged: <h4>
Unchanged: Less Than or Equals To - lteqUnchanged: Less Than or Equals To - lteq
Unchanged: </h4>Unchanged: </h4>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;lteq&#39; =&gt; 5));</code></pre>Unchanged: <code>$_products- &gt;addAttributeToFilter( &#39;id&#39;, array(&#39;lteq&#39; =&gt; 5));</code></pre>
Unchanged: <h3>Unchanged: <h3>
Unchanged: addFieldToFilter()Unchanged: addFieldToFilter()
Unchanged: </h3>Unchanged: </h3>
Unchanged: <p>Unchanged: <p>
Unchanged: addAttributeToFilter only works with EAV entity types (products, categories, customers etc) in Magento. To use the same functionality with non-eav entity models, swap the addAttributeToFilter() method with addFieldToFilter(). This functions works in the exact same way and takes the same parameters, however it works on non-EAV entities.Unchanged: addAttributeToFilter only works with EAV entity types (products, categories, customers etc) in Magento. To use the same functionality with non-eav entity models, swap the addAttributeToFilter() method with addFieldToFilter(). This functions works in the exact same way and takes the same parameters, however it works on non-EAV entities.
Unchanged: </p>Unchanged: </p>
Unchanged: <h3>Unchanged: <h3>
Unchanged: Debugging The SQL QueryUnchanged: Debugging The SQL Query
Unchanged: </h3>Unchanged: </h3>
Unchanged: <p>Unchanged: <p>
Unchanged: There are two ways to debug the query being executed when loading a collection in Magento.Unchanged: There are two ways to debug the query being executed when loading a collection in Magento.
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>// Method 1Unchanged: <code>// Method 1
Unchanged: Mage::getResourceModel( &#39;catalog/ product_collection&#39; )-&gt;load(true);Unchanged: Mage::getResourceModel( &#39;catalog/ product_collection&#39; )-&gt;load(true);
Unchanged: // Method 2Unchanged: // Method 2
Unchanged: $collection = Mage::getResourceModel( &#39;catalog/ product_collection&#39;)Unchanged: $collection = Mage::getResourceModel( &#39;catalog/ product_collection&#39;)
Unchanged: echo $collection-&gt; getSelect();< /code></pre>Unchanged: echo $collection-&gt; getSelect();< /code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much quicker but I include them both here for reference.Unchanged: Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much quicker but I include them both here for reference.
Unchanged: </p>Unchanged: </p>

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply