Jan 8, 2012

Magento: Mysql records with NULL values are not fetched in query

After banging my head on my desk trying to get the records with NULL values with Magento ORM, it was found that writing

$collection->addAttributeToFilter('somefield', 'null')
$collection->addAttributeToFilter('somefield', array('is' => 'null'))

will check for any blank values like this: WHERE somefield = ”

So if you want to fetch records that have NULL values in Magento style, you need to write as following:

$collection->addAttributeToFilter('somefield', array('null'=>'null')

will check like WHERE somefield = null

Hope this saves someone’s time!


  • It’s actually not critical that the array value be the string “null”. The value just needs to resolve to true.

    • Agree, thanks.

Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors