AddAttributeToFilter( Contionals - Getting products from the
table in magento commerce ADDATTRIBUTETOFILTER CONDITIONALS IN MAGENTO
addAttributeToFilter
is a function that can be called on a product collection in Magento. In short,
it adds a condition to the WHERE part of the MySQL query used to extract a
product collection from the database.
1
2
3
4
|
$_products =
Mage::getModel('catalog/product')->getCollection()
->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, 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
1
|
$_products->addAttributeToFilter('status', array('eq'
=>
1));
|
Not Equals - neq
1
|
$_products->addAttributeToFilter('sku', array('neq'
=>
'test-product'));
|
Like - like
1
|
$_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.
Not Like - nlike
1
|
$_products->addAttributeToFilter('sku', array('nlike'
=>
'err-prod%'));
|
In - in
1
|
$_products->addAttributeToFilter('id', array('in'
=>
array(1,4,98)));
|
When using in,
the value parameter accepts an array of values.
Not In - nin
1
|
$_products->addAttributeToFilter('id', array('nin'
=>
array(1,4,98)));
|
NULL - null
1
|
$_products->addAttributeToFilter('description', 'null');
|
Not NULL - notnull
1
|
$_products->addAttributeToFilter('description', 'notnull');
|
Greater Than - gt
1
|
$_products->addAttributeToFilter('id', array('gt'
=>
5));
|
Less Than - lt
1
|
$_products->addAttributeToFilter('id', array('lt'
=>
5));
|
Greater Than or
Equals To- gteq
1
|
$_products->addAttributeToFilter('id', array('gteq'
=>
5));
|
Less Than or
Equals To - lteq
1
|
$_products->addAttributeToFilter('id', array('lteq'
=>
5));
|
addFieldToFilter()
Debugging The SQL Query
There are two ways to debug the query being
executed when loading a collection in Magento.
1
2
3
4
5
6
7
|
// Method 1
Mage::getModel('catalog/product')->getCollection()->load(true);
// Method 2 (Quicker, Recommended)
$collection =
Mage::getModel('catalog/product')->getCollection();
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
36 comments:
Nice blog...Very useful information is providing by ur blog. Great beginning php tutorials Very clear and helpful for beginners.
I must thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade content by you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now..
Informatica Training in Chennai
Selenium Training in Chennai
php opensource is very useful to everyone and useful to me really.thank u for ur post.Vmware Training in Chennai
product is very useful and learning think is very good .
Vmware Training in Chennai
this product is very useful for every one easwwily understanding .and thank u for your post..
Vmware Training in Chennai
I have really enjoyed reading your blog posts. This information is impressive. I am inspired with your post.. I hope you post again soon. photos very nice.
J2EE Training in Chennai
Thank you for your sharing information..Its very interesting and useful..
SAP Training in Chennai
Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
AP ABAP Training in Chennai
I have really enjoyed reading your blog posts. This information is impressive. I am inspired with your post.. I hope you post again soon. photos very nice
Digital Marketing Company in Chennai
I really thinking about the blog. This is very impressive and technically useful for the peeple those who are going to use it.
Digital Marketing Training in Chennai
this awesome helpful blog generally.thank u for you post.
cloud computing training in chennai
There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this.
Cloud Computing Training in Chennai
so nice to hear about this type of blog. This will be amaze the people-:)
SAP ABAP Training in Chennai
SAP MM Training in Chennai
SAP HR Training in Chennai
very informative blog and very useful for readers. making us to learn more from your blog.
java training in chennai
dotnet training in chennai
cloud computing training in chennai
digital marketing training in chennai
Really i like this blog and i got lot of information's from your blog.And thanks for sharing!!!!
Home Interiors in Chennai
Just read your website. Good one. I liked it. Keep going. you are a best writer your site is very useful and informative thanks for sharing!
Peridotsystems
Interesting blog post.This blog shows that you have a great future as a content writer.waiting for more updates.
Peridotsystems
Hadoop Training in Chennai
Digital Marketing Course in Chennai
Data Science Course in Chennai
Tableau Training Course in Chennai
I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job .I feel happy to read your post.Thanks for sharing.
Dataware Tools
HADOOP Training in Chennai
Base SAS Training in Chennai
MSBI Training in Chennai
INFORMATICA Training in Chennai
DATAWAREHOUSE Training in Chennai
Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much.
Acne Cream | Psoriasis Scalp Treatment |
Best Anti Dandruff Shampoo
Dry Skin Treatment | Dandruff Shampoo
Thanks for sharing this valuable information.I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job.
SAP Training Institute in Chennai
SAP HR Training in Chennai
SAP SD Training in Chennai
BEST SAP BASIS Training in Chennai
Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, very nice article.
Iron and Steel Dealers in Tirupati
The blog is very different and useful. The way of writing makes it more interesting.
Selenium Training in Chennai
Great.Nice information.It is more useful and knowledgeable. Thanks for sharing keep going on.
Digital Marketing Company in Chennai
Great.Nice information.It is more useful and knowledgeable. Thanks for sharing keep going on...
Hadoop training in chennai
DigitL Mrketing course in chennai
AWS training in chennai
Good information. It was really useful. looking forward for more useful information
digital marketing company in chennai
It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
Web design company in chennai
i have really enjoyed sharing your website. thank you so much for your sharing this document. this document more useful and improve our knowledge.
salesforce training in chennai
Its really very useful topic. Thanks for sharing this information...
SAP FICO Training in Chennai
nice post!!
SAP FICO Training in Chennai
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing
SAP HR Training in Chennai
SAP SD Training in Chennai
NICE ONE...CONTINUE AS IT IS..
Isoft Innovations Facebook | Isoft innovations faq | Isoft Innovations About Us | Isoft Innovations Address | Isoft Innovations Contact Us |isoft innovations Software Company | Isoft Innovations Chennai | Isoft Innovations Employees | Isoft Innovations Careers | Isoft Innovations Consulting Services
Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
Best Interior Designers in Chennai
Industrial Architecture
Warehouse Architect
Factory Architect Chennai
https://way2discuss.blogspot.com/2013/11/magento-addattributetofilter-contionals.html?showComment=1530246901793#c8573909475217872767
http://ibdpartnership.blogspot.com/SQream Technologies provides you with a state of the art software which combines modern GPU technology (Graphic Processing Units) with the best practices in today’s Big Data platforms, providing up to 100x faster insights from data.
Bigdata Training in Chennai OMR
Dil Bechara 2020 FHD Download Here
Sushant Singh Rajput Last Movie Dil Bechara 2020 Download HDRip
Post a Comment